python - 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))
解决方案
combinations
按排序顺序为您提供元组,没有重复。听起来你想要itertools.product
:
from itertools import product
atom = range(11)
print(len(list(product(atom, repeat=4))))
# 14641
推荐阅读
- django - 如何使用 Django ORM 将注释字符串转换为布尔值
- architecture - 我是否也应该记录当前的上下文和范围
- export - 定期将数据从 SAP 导出到其他软件的首选方式是什么?
- javascript - 向从 api 响应获得的字符串添加其他字符
- python - 来自 TF2 的 tf.print 不打印到输出
- c# - 在 MVC asp.net 中验证出生日期
- javascript - slick-carousel scss 无法与使用 webpack 作为捆绑器的 react-slick 一起正常工作
- google-cloud-platform - 在使用 Cloud Run 和 Stackdriver 时,Micrometer 自定义指标如何适用于具有少量短调用的容器
- performance - 在 SPARQL 屏蔽结果中排序
- maven - Maven 插件可以使用构建依赖项吗?