两个python字母和数字列表的笛卡尔乘积

两个python字母和数字列表的笛卡尔乘积,python,pandas,list,loops,Python,Pandas,List,Loops,当前 渴望的 list1 = ['A','B','C'] list2 = [1,2,3] 我试过的 list 3 = [['A', 1], ['A', 2], ['A', 3], ['B', 1], ['B', 2],['B', 3], ['C', 1], ['C', 2], ['C', 3]] 结果: list3 = [l+str(n) for l in list1 for n in list2] 你很接近: ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', '

当前

渴望的

list1 = ['A','B','C']
list2 = [1,2,3]
我试过的

list 3 = [['A', 1], ['A', 2], ['A', 3], ['B', 1], ['B', 2],['B', 3],
 ['C', 1], ['C', 2], ['C', 3]]
结果:

list3 = [l+str(n) for l in list1 for n in list2]
你很接近:

['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']

正如Mark已经提到的,itertools.product将是一条可行之路。但是如果您想继续使用列表理解,正确的行应该是

[[l,n] for l in list1 for n in list2]

为什么不直接使用
itertools.product
list3=[[l,str(n)]表示列表1中的l表示列表2中的n]
注意第一部分周围的括号
list3 = [ [l, str(n)] for l in list1 for n in list2 ]
[[l]+[n] for l in list1 for n in list2]