Python将第一个列表的每个条目与第二个列表的每个条目结合在一起

问题描述 投票:-2回答:1

我想将二维列表放入函数中,以便它返回一个二维列表,其中每个第二个列表项都组合在一起,如下所示:

List2D = [['a','b','c'], [1, 2, 3]]

def MyFunction(List2D):
    ...
    (some code here)
    ...
    print(TheFinalList)

所需的输出:[['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3']]

python python-3.x nested-lists
1个回答
0
投票

有人删除了正确答案的帖子:

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

© www.soinside.com 2019 - 2024. All rights reserved.