一个网友定制的Python快乐8组号代码

时间:2025-05-07 10:45:35 来源:淘折扣  阅读:(12) 收藏
转载:

Python快乐8组号代码:我们需要在生成组合时,确保在每个步骤中,如果当前组合已经满足9个数,就不再继续添加更多的组,而是直接跳到下一个组合。

Python快乐8组号代码:我们需要在生成组合时,确保在每个步骤中,如果当前组合已经满足9个数,就不再继续添加更多的组,而是直接跳到下一个组合。如果当前组合不足9个数,则继续添加下一个组,直到凑够9个数。

一个网友定制的Python快乐8组号代码

快乐8组号代码

from itertools import combinations

# 初始化数字填入快乐8预选号码组
numbers = [
    1, 3, 5, 6, 9, 13, 14, 15, 16,
    17, 21, 24, 38, 41, 46, 49, 51, 52,
    55, 57, 59, 62, 64, 65, 67, 68, 69,
    72, 75
]

# 分类数字按照数字1到80分成八个集合
grouped_numbers = {
    'A': [num for num in numbers if 1 <= num <= 9],   # 1-9
    'B': [num for num in numbers if 10 <= num <= 19], # 10-19
    'C': [num for num in numbers if 20 <= num <= 29], # 20-29
    'D': [num for num in numbers if 30 <= num <= 39], # 30-39
    'E': [num for num in numbers if 40 <= num <= 49], # 40-49
    'F': [num for num in numbers if 50 <= num <= 59], # 50-59
    'G': [num for num in numbers if 60 <= num <= 69], # 60-69
    'H': [num for num in numbers if 70 <= num <= 80]  # 70-80
}

# 用于存储组合结果
all_combinations = set()  # 使用集合避免重复组合

def generate_combinations():
    # 首先固定'1-9'的数字组
    base_numbers = grouped_numbers['A']
    
    # 依次从每个区间中选择数字,直到凑成九个数
    groups = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
    
    for i in range(len(groups)):
        current_group = groups[i]
        remaining_count = 9 - len(base_numbers)
        
        # 如果当前组的数字数量足够补充到9个数
        if len(grouped_numbers[current_group]) >= remaining_count:
            for combo in combinations(grouped_numbers[current_group], remaining_count):
                combination = base_numbers + list(combo)
                all_combinations.add(tuple(combination))
        else:
            # 如果当前组的数字数量不足,尝试继续添加下一个组
            temp_numbers = base_numbers + grouped_numbers[current_group]
            for j in range(i + 1, len(groups)):
                next_group = groups[j]
                remaining_count = 9 - len(temp_numbers)
                if len(grouped_numbers[next_group]) >= remaining_count:
                    for combo in combinations(grouped_numbers[next_group], remaining_count):
                        combination = temp_numbers + list(combo)
                        all_combinations.add(tuple(combination))
                    break  # 一旦找到足够的数字,就停止添加更多组
                else:
                    temp_numbers += grouped_numbers[next_group]

generate_combinations()

# 打印所有组合
for combo in sorted(all_combinations):
    print(list(combo))

# 打印组合总数
print("\n总组合数:", len(all_combinations))

正在运行程序...

一个网友定制的Python快乐8组号代码

快乐8代码

[1, 3, 5, 6, 9, 13, 14, 15, 16]

[1, 3, 5, 6, 9, 13, 14, 15, 17]

[1, 3, 5, 6, 9, 13, 14, 16, 17]

[1, 3, 5, 6, 9, 13, 15, 16, 17]

[1, 3, 5, 6, 9, 14, 15, 16, 17]

[1, 3, 5, 6, 9, 21, 24, 38, 41]

[1, 3, 5, 6, 9, 21, 24, 38, 46]

[1, 3, 5, 6, 9, 21, 24, 38, 49]

[1, 3, 5, 6, 9, 38, 41, 46, 49]

[1, 3, 5, 6, 9, 41, 46, 49, 51]

[1, 3, 5, 6, 9, 41, 46, 49, 52]

[1, 3, 5, 6, 9, 41, 46, 49, 55]

[1, 3, 5, 6, 9, 41, 46, 49, 57]

[1, 3, 5, 6, 9, 41, 46, 49, 59]

[1, 3, 5, 6, 9, 51, 52, 55, 57]

[1, 3, 5, 6, 9, 51, 52, 55, 59]

[1, 3, 5, 6, 9, 51, 52, 57, 59]

[1, 3, 5, 6, 9, 51, 55, 57, 59]

[1, 3, 5, 6, 9, 52, 55, 57, 59]

[1, 3, 5, 6, 9, 62, 64, 65, 67]

[1, 3, 5, 6, 9, 62, 64, 65, 68]

[1, 3, 5, 6, 9, 62, 64, 65, 69]

[1, 3, 5, 6, 9, 62, 64, 67, 68]

[1, 3, 5, 6, 9, 62, 64, 67, 69]

[1, 3, 5, 6, 9, 62, 64, 68, 69]

[1, 3, 5, 6, 9, 62, 65, 67, 68]

[1, 3, 5, 6, 9, 62, 65, 67, 69]

[1, 3, 5, 6, 9, 62, 65, 68, 69]

[1, 3, 5, 6, 9, 62, 67, 68, 69]

[1, 3, 5, 6, 9, 64, 65, 67, 68]

[1, 3, 5, 6, 9, 64, 65, 67, 69]

[1, 3, 5, 6, 9, 64, 65, 68, 69]

[1, 3, 5, 6, 9, 64, 67, 68, 69]

[1, 3, 5, 6, 9, 65, 67, 68, 69]

总组合数: 34

程序运行结束。

如果需要不同的集合作为固定组合,比如选定11到19的作为胆,那么把27行的A和31一行的B调换一下就可以了。

标签:

热门排行

猜你喜欢

热门标签

扫描二维码打开

周一至周六

9:00-22:00                  

淘折扣  滇ICP备2023000592号-3  滇公网安备53230102000530号   统一社会信用代码:91532300MAC2D0R706 Copyright © 2010 - 2025 https://www.bgaw.cn/ All Rights Reserved