我有以下清单:
# list_1 : n=100k
# list_2 : n=200k
list_1 = [['name1', '001'], ['name2', '001'], ...]
list_2 = [['other1', '003'], ['other2', '005'], ...]我希望将它们合并到下面,同时在耗尽任何一个列表时停止(如zip()所示):
combined_list = [['name1', '001', 'other1', '003'], ['name2', '001', 'other2', '005']]我尝试过zip(),但是这会为每个组合子列表生成一个由两个列表组成的元组。
是否有一种方法可以简洁地实现这一点(而不需要在zip()之后再循环)?
发布于 2022-06-03 20:57:19
你试过什么密码?我怀疑您对如何调用zip()有异议。
这将在每个索引上将两个列表一起添加,使用zip()。
list_1 = [['name1', '001'], ['name2', '001']]
list_2 = [['other1', '003'], ['other2', '005']]
combined_list = [x + y for x, y in zip(list_1, list_2)]
print(combined_list)[['name1', '001', 'other1', '003'], ['name2', '001', 'other2', '005']]https://stackoverflow.com/questions/72495075
复制相似问题