首页 > 解决方案 > python itertool组合列表不完整

问题描述

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

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

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

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

标签: pythoncombinationsitertools

解决方案


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

from itertools import product
atom = range(11)

print(len(list(product(atom, repeat=4))))
# 14641

推荐阅读