将 itertools 的结果放在一个列表中
Put results of itertools in one single list
我有这段代码,我正试图将 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
你可以这样做:
from itertools import product
list(map(''.join, product('0123456789', repeat=2)))
# ['00', '01', '02', '03', '04', '05', '06', '07', ...]
上面的代码是您创建列表的每次迭代。要将元素放入一个列表,请创建一个空列表,将项目附加到每个数组中的列表。实际上你不需要 itertool
这个...
strng = '0123456789'
num = []
for r in strng :
num.append(r)
print(num)
但是如果你真的想用itertools
,你可以用这个
import itertools as iter
num = []
for r in iter.chain('0123456789') :
num.append(r)
print(num)
我有这段代码,我正试图将 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
你可以这样做:
from itertools import product
list(map(''.join, product('0123456789', repeat=2)))
# ['00', '01', '02', '03', '04', '05', '06', '07', ...]
上面的代码是您创建列表的每次迭代。要将元素放入一个列表,请创建一个空列表,将项目附加到每个数组中的列表。实际上你不需要 itertool
这个...
strng = '0123456789'
num = []
for r in strng :
num.append(r)
print(num)
但是如果你真的想用itertools
,你可以用这个
import itertools as iter
num = []
for r in iter.chain('0123456789') :
num.append(r)
print(num)