我有一种迭代列表的方法,如我所希望的:
a = ["1","2","3","4","5","6","7","8","9","10"]
b = ['A','B','C','D','E','F','G','H','I','J']
c = ["11","12","13","14","15","16","17","18","19","20"]
for x, y, z in [(x,y,z) for x in a for y in b for z in c]:
print(x,y,z)输出:
1 A 11
1 A 12
1 A 13
1 A 14
1 A 15
1 A 16
1 A 17
1 A 18
1 A 19
1 A 20
1 B 11
1 B 12
1 B 13
1 B 14
...etc但是,如果我的列表存储在一个列表中,并且有n个列表,那么如何实现相同的结果呢?例如:
main_list=[["1","2","3","4","5","6","7","8","9","1"],['A','B','C','D','E','F','G','H','I','J'],["11","12","13","14","15","16","17","18","19","20"],['k','l','m','n','o','p','q','r','s','t']]提前谢谢。
发布于 2020-05-30 22:22:56
a = ["1","2","3","4","5","6","7","8","9","10"]
b = ['A','B','C','D','E','F','G','H','I','J']
c = ["11","12","13","14","15","16","17","18","19","20"]
from itertools import product
all_lists = [a, b, c]
for c in product(*all_lists):
print(c)指纹:
('1', 'A', '11')
('1', 'A', '12')
('1', 'A', '13')
('1', 'A', '14')
('1', 'A', '15')
('1', 'A', '16')
('1', 'A', '17')
('1', 'A', '18')
('1', 'A', '19')
('1', 'A', '20')
('1', 'B', '11')
... and so on.发布于 2020-05-30 22:24:25
对于该示例,下面给出了所需的组合。
import itertools
list(itertools.product(*(a,b,c)))对于主犯来说-
main_list=[["1","2","3","4","5","6","7","8","9","1"],['A','B','C','D','E','F','G','H','I','J'],["11","12","13","14","15","16","17","18","19","20"],['k','l','m','n','o','p','q','r','s','t']]
combintns = list(itertools.product(*main_list))
# in case you're specific about output format/appearance
for i in combintns:
print(i[0],i[1],i[2],i[3])https://stackoverflow.com/questions/62109202
复制相似问题