当前
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']
答案 0 :(得分:1)
您很亲密:
[[l,n] for l in list1 for n in list2]
答案 1 :(得分:0)
[[l]+[n] for l in list1 for n in list2]
答案 2 :(得分:-1)
正如Mark所提到的,itertools.product将是必经之路。但是,如果您想继续使用列表理解功能,则正确的行应该是
list3 = [ [l, str(n)] for l in list1 for n in list2 ]