python itertool组合列表不完整

DCh*_*aka 1 python combinations python-itertools

我有一个包含 11 个元素的列表,我需要其中长度为 4 的所有可能的元组。于是我在Itertools中找到了这个函数combinations

然而,它只提供 210 个元组,而不是 11^4 = 14641。我检查了该print函数,发现其中许多元组丢失了。

我能做什么,或者有什么问题?

atom = [0, 5, 6, 12, 10, 13, 11, 9, 1, 2]
atoms = list(itertools.combinations(atom,4))
Run Code Online (Sandbox Code Playgroud)

Pat*_*ugh 5

combinations按排序顺序为您提供元组,没有重复。听起来你反而想要itertools.product

from itertools import product
atom = range(11)

print(len(list(product(atom, repeat=4))))
# 14641
Run Code Online (Sandbox Code Playgroud)