【发布时间】:2020-05-26 09:45:59
【问题描述】:
所以,我有这个非常基本的代码:
t = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,90]
c = list(itertools.combinations(t, 5))
print(c)
当我运行它大约需要 15 秒,然后它会给出以下错误:
Traceback (most recent call last):
File "H:/<path>/main.py", line 13, in <module>
print(c)
OSError: [Errno 22] Invalid argument
但是,当我运行这段代码时:
t = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10]
c = list(itertools.combinations(t, 5))
print(c)
它没有给我任何错误,而是我想要的输出:
[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 4, 7), ... (10, 7, 8, 9, 10), (6, 7, 8, 9, 10)]
为什么它不适用于列表t 中的更多数字?
我不坚持itertools,所以你可以给我看另一个这样做的例子。它只需要给出每个组合而不重复。
【问题讨论】:
-
为什么要把所有的组合都放到一个列表中?
-
包含约 40m 元组的列表可能对您的系统/控制台来说太多了……?
-
列表
c的大小是43,949,268元组5元素。我猜你的操作系统在打印这个时有一点问题(可以理解......) -
尝试分别打印每个元组。
标签: python python-3.x combinations itertools combinatorics