这是一个仅使用标准库中的模块的简单解决方案:
from itertools import combinations
from collections import Counter
draws = [
[13, 14, 28, 30, 31, 37, 39],
[7, 10, 12, 16, 21, 22, 33],
[1, 2, 7, 15, 25, 31, 33],
[3, 6, 18, 21, 31, 34, 39]
]
duos = Counter()
trios = Counter()
for draw in draws:
duos.update(combinations(draw, 2))
trios.update(combinations(draw, 3))
print('Top 5 duos')
for x in duos.most_common(5):
print(f'{x[0]}: {x[1]}')
print()
print('Top 5 trios')
for x in trios.most_common(5):
print(f'{x[0]}: {x[1]}')
上面的代码 sn-p 将产生以下输出:
Top 5 duos
(31, 39): 2
(7, 33): 2
(13, 14): 1
(13, 28): 1
(13, 30): 1
Top 5 trios
(13, 14, 28): 1
(13, 14, 30): 1
(13, 14, 31): 1
(13, 14, 37): 1
(13, 14, 39): 1
这里还有一点优雅的版本:
from itertools import combinations
from collections import Counter
draws = [
[13, 14, 28, 30, 31, 37, 39],
[7, 10, 12, 16, 21, 22, 33],
[1, 2, 7, 15, 25, 31, 33],
[3, 6, 18, 21, 31, 34, 39]
]
counters = [Counter() for _ in range(3)]
for n, counter in enumerate(counters, 2):
for draw in draws:
counter.update(combinations(draw, n))
print(f'Top 10 combos of {n} numbers')
for combo, count in counter.most_common(10):
print(' '.join((f'{_:2d}' for _ in combo)), count, sep=': ')
print()
这将为我们提供以下输出:
Top 10 combos of 2 numbers
31 39: 2
7 33: 2
13 14: 1
13 28: 1
13 30: 1
13 31: 1
13 37: 1
13 39: 1
14 28: 1
14 30: 1
Top 10 combos of 3 numbers
13 14 28: 1
13 14 30: 1
13 14 31: 1
13 14 37: 1
13 14 39: 1
13 28 30: 1
13 28 31: 1
13 28 37: 1
13 28 39: 1
13 30 31: 1
Top 10 combos of 4 numbers
13 14 28 30: 1
13 14 28 31: 1
13 14 28 37: 1
13 14 28 39: 1
13 14 30 31: 1
13 14 30 37: 1
13 14 30 39: 1
13 14 31 37: 1
13 14 31 39: 1
13 14 37 39: 1