如何在python中合并两个不相等的嵌套列表?

2024-05-29 03:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

list1 = [['apple','b','c'] ,['dolly','e','f']]
list2 =[['awsme','b','c'] ,['dad','e','f'],['tally','e','f']]


list_combine = [item for sublst in zip(list1, list2) for item in sublst]
print(list_combine)


Expected Output:

list_combine = [['apple','b','c'] ,['dolly','e','f'],['awsme','b','c'] ,['dad','e','f'],['tally','e','f']]

如何在python中将两个不相等的嵌套列表合并为单个嵌套列表


Tags: inapple列表forzipitemlistcombine
2条回答

您可以通过定义一个新变量,如list3或任何您调用的变量,将这两个列表连接起来

同样由于PEP8,我只是以一种更具python风格的方式修改了您的代码,使其更具可读性。不建议在逗号前加空格,但建议在逗号后加空格。这项建议不仅是用Python编写的,而且也是用英语语法编写的更好方法

如果您对我的回答有任何疑问和问题,请查看此信息并通知我:

list1 = [['apple', 'b', 'c'], ['dolly', 'e', 'f']]
list2 = [['awsme', 'b', 'c'], ['dad', 'e', 'f'], ['tally', 'e', 'f']]
list3 = list1 + list2
print(list3)

我希望它会有用

您只需使用“+”运算符联接两个列表

list_combine = list1 + list2
print(list_combine)

输出

list_combine = [['apple','b','c'] ,['dolly','e','f'],['awsme','b','c'] ,['dad','e','f'],['tally','e','f']]

相关问题 更多 >

    热门问题