简体   繁体   English

连接 Python 中列表列表中的列表

[英]Concatenate the lists inside a list of lists in Python

I want to "concatenate" 2 lists of the form [[a], [b], [c], ...] and [[e], [f], [g], ...] to obtain a list [[a,e], [b,f], [c,g], ...] .我想“连接” [[a], [b], [c], ...][[e], [f], [g], ...]形式的 2 个列表以获得列表[[a,e], [b,f], [c,g], ...] The lengths of the 2 lists (n lists) will always be the same. 2 个列表(n 个列表)的长度将始终相同。 How can I do this?我怎样才能做到这一点? I'm new to Python:(我是 Python 的新手:(

you use zip to iterate through the elements of both list at same time, then add them together (list+list = concatenate the 2 list), and finaly append the result to a third list (concatenated list):您使用 zip 同时遍历两个列表的元素,然后将它们加在一起(列表 + 列表 = 连接 2 列表),最后 append 将结果添加到第三个列表(连接列表):

Something like this:像这样的东西:

l1, l2 = [['a'], ['b'], ['c']], [['e'], ['f'], ['j']]
concat = []
for i, j in zip(l1, l2):
    concat.append(i + j)
print(concat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM