我有一个包含 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))
最佳答案
combinations
为您提供按排序顺序的元组,没有重复。听起来您想要 itertools.product
:
from itertools import product
atom = range(11)
print(len(list(product(atom, repeat=4))))
# 14641
关于python itertool组合列表不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53173993/