【发布时间】:2018-08-09 20:19:29
【问题描述】:
我有这段代码,我正在尝试将 itertools 的所有结果放在一个列表中
:l = ['00','01','02','03'..],而不是我在每一行都得到一个列表['0', '0'] ['0', '1'] ['0', '2'] ['0', '3']
import itertools
for r in itertools.product('0123456789', repeat=2):
print list(r)
【问题讨论】:
-
为什么需要
itertools? -
list('0123456789')? -
不确定为什么要使用 itertools,但这是一种过度设计的方法:
[el for r in itertools.product('0123456789', repeat=1) for el in r] -
谢谢,但在 repeat='2' 的情况下,它似乎没有返回我所期望的:l = ['00', '01', '02', ' 03' . . . ]
-
@Olivier 好吧,检查我的更新。
标签: python list itertools cartesian-product