selected_combinations = [list(red_ball_combination) + [blue_ball] for red_ball_combination in itertools.combinations(red_balls, 6) if not set([4, 8, 17, 20, 22, 27]).intersection(set(red_ball_combination)) for blue_ball in blue_balls]把这句更改为没有相同的数据才保存
时间: 2024-03-25 09:41:41 浏览: 84
可以使用一个集合来保存已经生成的组合,判断新的组合是否重复即可。修改后的代码如下:
```python
import itertools
red_balls = list(range(1, 34))
blue_balls = list(range(1, 17))
n = 0
selected_combinations = []
generated_combinations = set()
for red_ball_combination in itertools.combinations(red_balls, 6):
if set([4, 8, 17, 20, 22, 27]).intersection(set(red_ball_combination)):
continue
for blue_ball in blue_balls:
combination = list(red_ball_combination) + [blue_ball]
if tuple(combination) not in generated_combinations:
selected_combinations.append(combination)
generated_combinations.add(tuple(combination))
for i, combination in enumerate(selected_combinations):
hongqiu = combination[:6]
lanqiu = combination[-1]
max_consecutive = 1
zdxl = 1
for j in range(len(hongqiu) - 1):
if hongqiu[j + 1] == hongqiu[j] + 1:
zdxl += 1
if zdxl > max_consecutive:
max_consecutive = zdxl
else:
zdxl = 1
if max_consecutive <= 3:
n += 1
print(f"红球: {hongqiu}, 篮球: {lanqiu}, 第{n}组")
```
这样修改后,可以确保不会有重复的组合出现。
阅读全文