You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python技术实现:乐透模拟器奖项统计及列表数字匹配判断

Hey there! Let's tackle these two Python problems one by one. I'll break down the logic clearly and share working code with explanations so you can follow along easily.

Problem 1: Check if numbers from one list exist in another list

First, let's cover two common scenarios here—since the question can be interpreted a couple of ways:

Scenario 1: Check each element individually

If you want a result for every number in your target list showing whether it's present in the reference list, a list comprehension with the in operator is the way to go. It's simple and readable.

def check_elements_exist(target_list, reference_list):
    # Returns a list of booleans matching each element in target_list
    return [num in reference_list for num in target_list]

# Example usage
list_a = [2, 5, 9, 12]
list_b = [5, 10, 12, 15]
print(check_elements_exist(list_a, list_b))  # Output: [False, True, False, True]

Scenario 2: Verify all elements in the target list exist in the reference list

If you need to confirm that every number in your target list is present in the reference list (i.e., the target is a subset of the reference), use all() with a generator expression. This gives you a single True/False result.

def all_elements_exist(target_list, reference_list):
    # Returns True if every element in target_list is in reference_list, else False
    return all(num in reference_list for num in target_list)

# Example usage
list_c = [5, 12]
list_d = [5, 10, 12, 15]
print(all_elements_exist(list_c, list_d))  # Output: True
print(all_elements_exist(list_a, list_d))  # Output: False

Pro tip: For large lists, convert the reference list to a set first—this makes in checks run in O(1) time instead of O(n), which is a huge speed boost. Here's how to adjust the functions:

def check_elements_exist(target_list, reference_list):
    ref_set = set(reference_list)
    return [num in ref_set for num in target_list]

Problem 2: Lottery Simulator

This one's a bit more involved, but let's break it into manageable steps:

  1. Generate a winning set of 6 unique numbers from 1-45
  2. Create 1000 player entries (each also 6 unique numbers)
  3. Count how many numbers each player matches with the winning set
  4. Tally up winners for each prize tier (6 matches = 1st prize, 5 = 2nd, etc.)

Here's the complete, tested code:

import random

def generate_lottery_numbers():
    # Generate 6 unique random numbers between 1 and 45, sorted for readability
    return sorted(random.sample(range(1, 46), 6))

def count_matches(player_numbers, winning_numbers):
    # Use sets to quickly find common numbers (intersection)
    player_set = set(player_numbers)
    winning_set = set(winning_numbers)
    return len(player_set.intersection(winning_set))

def run_lottery_simulator(num_players=1000):
    # Step 1: Generate the winning numbers
    winning_numbers = generate_lottery_numbers()
    print(f"Winning numbers: {winning_numbers}")
    
    # Step 2: Set up a dictionary to track prize counts
    prize_counts = {
        6: 0,  # 1st Prize (all 6 matches)
        5: 0,  # 2nd Prize
        4: 0,  # 3rd Prize
        3: 0,  # 4th Prize
        2: 0,  # 5th Prize
        1: 0,  # 6th Prize
        0: 0   # No prize
    }
    
    # Step 3: Generate player entries and count matches
    for _ in range(num_players):
        player_numbers = generate_lottery_numbers()
        match_count = count_matches(player_numbers, winning_numbers)
        prize_counts[match_count] += 1
    
    # Step 4: Print out the final results
    print("\nLottery Results:")
    print(f"1st Prize (6 matches): {prize_counts[6]} winner(s)")
    print(f"2nd Prize (5 matches): {prize_counts[5]} winner(s)")
    print(f"3rd Prize (4 matches): {prize_counts[4]} winner(s)")
    print(f"4th Prize (3 matches): {prize_counts[3]} winner(s)")
    print(f"5th Prize (2 matches): {prize_counts[2]} winner(s)")
    print(f"6th Prize (1 match): {prize_counts[1]} winner(s)")
    print(f"No Prize (0 matches): {prize_counts[0]} players")

# Run the simulator
run_lottery_simulator()

Quick notes on the code:

  • random.sample() is ideal here because it selects unique elements without replacement—exactly what we need for lottery numbers (no duplicates allowed!).
  • Using sets for match counting is way more efficient than looping through each number manually. The intersection of two sets gives us all common numbers, and len() tells us how many matches there are.
  • The prize_counts dictionary makes it easy to track each tier. If you don't care about counting players with 0 matches, just remove that key from the dictionary.

内容的提问来源于stack exchange,提问作者MacGenius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:54:36