Python 将第一个列表的每个条目与第二个列表的每个条目合并
Python combine every entry of the first list with every entry of the second
我想将一个二维列表放入一个函数中,以便它 returns 一个二维列表,每个第二个列表条目都组合在一起,如下所示:
List2D = [['a','b','c'], [1, 2, 3]]
def MyFunction(List2D):
...
(some code here)
...
print(TheFinalList)
desired output: [['a1','a2','a3'], ['b1','b2','b3'], ['c1','c2','c3']]
有人用正确答案删除了他的 post:
NewList2D = [['a', 'b', 'c'], [1, 2, 3]]
List = [[str(x)+str(y) for y in NewList2D[1]] for x in NewList2D[0]]
print(List)
output: [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']]
感谢-Nick
我想将一个二维列表放入一个函数中,以便它 returns 一个二维列表,每个第二个列表条目都组合在一起,如下所示:
List2D = [['a','b','c'], [1, 2, 3]]
def MyFunction(List2D):
...
(some code here)
...
print(TheFinalList)
desired output: [['a1','a2','a3'], ['b1','b2','b3'], ['c1','c2','c3']]
有人用正确答案删除了他的 post:
NewList2D = [['a', 'b', 'c'], [1, 2, 3]]
List = [[str(x)+str(y) for y in NewList2D[1]] for x in NewList2D[0]]
print(List)
output: [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']]
感谢-Nick