合并2个列表列表

时间:2018-01-09 14:13:23

标签: python python-2.7 list

我希望合并2个列表列表。在第一段代码中,我可以合并2个没有重复元素的字符串列表!这就是我要找的东西,但有2个列表(字符串):

list1 = ['a', 'b', 'c', 'd']
list2 = ['c', 'd', 'e', 'f']
# List merge without dupe
list1.extend([element for element in list2 if element not in list1])
print list1
>>>
['a', 'b', 'c', 'd', 'e', 'f']

我在寻找什么:

list1=[["artur","1/1/2018","0.5"],
       ["paco","1/1/2018","2.11"],
       ["pepe","1/1/2018","11.2"],
       ["artur","2/1/2018","0.5"],
       ["paco","2/1/2018","2.11"],
       ["pepe","2/1/2018","11.2"]]

list2=[["artur","1/1/2018","Estable"],
       ["paco","1/1/2018","Critico"],
       ["pepe","1/1/2018","Critico"], 
       ["artur","2/1/2018","Estable"],
       ["paco","2/1/2018","Critico"],
       ["pepe","2/1/2018","Critico"]]

#desired output:
>>>list1
[["artur","1/1/2018","0.5","Estable"],
 ["paco","1/1/2018","2.11","Critico"],
 ["pepe","1/1/2018","11.2","Critico"],
 ["artur","2/1/2018","0.5","Estable"],
 ["paco","2/1/2018","2.11","Critico"],
 ["pepe","2/1/2018","11.2","Critico"]]

版:

如果我的列表是:

list1=[["artur,1/1/2018,0.5"],
       ["paco,1/1/2018,2.11"],
       ["pepe,1/1/2018,11.2"],
       ["artur,2/1/2018,0.5"],
       ["paco,2/1/2018,2.11"],
       ["pepe,2/1/2018,11.2"]]

list2=[["artur,1/1/2018,Estable"],
       ["paco,1/1/2018,Critico"],
       ["pepe,1/1/2018,Critico"], 
       ["artur,2/1/2018,Estable"],
       ["paco,2/1/2018,Critico"],
       ["pepe,2/1/2018,Critico"]]

我必须将csv转换为列表,但我有这样的列表,而不是我在问题的第一行中提出的问题!

3 个答案:

答案 0 :(得分:4)

一个嵌套的for循环检查每个子列表的前两个元素,并在找到匹配时附加:

for sub1 in list1:
    for sub2 in list2:
        if sub1[:2] == sub2[:2]:
            sub1.append(sub2[-1])
            continue

输出:

>>>list1
[['artur', '1/1/2018', '0.5', 'Estable'],
 ['paco', '1/1/2018', '2.11', 'Critico'],
 ['pepe', '1/1/2018', '11.2', 'Critico'],
 ['artur', '2/1/2018', '0.5', 'Estable'],
 ['paco', '2/1/2018', '2.11', 'Critico'],
 ['pepe', '2/1/2018', '11.2', 'Critico']]

答案 1 :(得分:1)

您可以使用for

进行enumerate()循环
list1=[["artur","1/1/2018","0.5"],
       ["paco","1/1/2018","2.11"],
       ["pepe","1/1/2018","11.2"],
       ["artur","2/1/2018","0.5"],
       ["paco","2/1/2018","2.11"],
       ["pepe","2/1/2018","11.2"]]

list2=[["artur","1/1/2018","Estable"],
       ["paco","1/1/2018","Critico"],
       ["pepe","1/1/2018","Critico"], 
       ["artur","2/1/2018","Estable"],
       ["paco","2/1/2018","Critico"],
       ["pepe","2/1/2018","Critico"]]


for i, item in enumerate(list2):
    list1[i].append(item[-1])

>>> pprint(list1)
[['artur', '1/1/2018', '0.5', 'Estable'],
 ['paco', '1/1/2018', '2.11', 'Critico'],
 ['pepe', '1/1/2018', '11.2', 'Critico'],
 ['artur', '2/1/2018', '0.5', 'Estable'],
 ['paco', '2/1/2018', '2.11', 'Critico'],
 ['pepe', '2/1/2018', '11.2', 'Critico']]

修改

list1=[["artur,1/1/2018,0.5"],
       ["paco,1/1/2018,2.11"],
       ["pepe,1/1/2018,11.2"],
       ["artur,2/1/2018,0.5"],
       ["paco,2/1/2018,2.11"],
       ["pepe,2/1/2018,11.2"]]

list2=[["artur,1/1/2018,Estable"],
       ["paco,1/1/2018,Critico"],
       ["pepe,1/1/2018,Critico"], 
       ["artur,2/1/2018,Estable"],
       ["paco,2/1/2018,Critico"],
       ["pepe,2/1/2018,Critico"]]

# slice assignment
list1[:] = [''.join(item).split(',') for item in list1]
list2[:] = [''.join(item).split(',') for item in list2]

for i, item in enumerate(list2):
    list1[i].append(item[-1])

>>> pprint(list1)
[['artur', '1/1/2018', '0.5', 'Estable'],
 ['paco', '1/1/2018', '2.11', 'Critico'],
 ['pepe', '1/1/2018', '11.2', 'Critico'],
 ['artur', '2/1/2018', '0.5', 'Estable'],
 ['paco', '2/1/2018', '2.11', 'Critico'],
 ['pepe', '2/1/2018', '11.2', 'Critico']]

答案 2 :(得分:0)

尝试:

ziplist = list(zip(list1, list2))
outlist = []
for z in ziplist:
    for i in z[1]:
        if i not in z[0]:
            z[0].append(i)
    outlist.append(z[0])

print(outlist)

输出:

[['artur', '1/1/2018', '0.5', 'Estable'], 
 ['paco', '1/1/2018', '2.11', 'Critico'], 
 ['pepe', '1/1/2018', '11.2', 'Critico'], 
 ['artur', '2/1/2018', '0.5', 'Estable'], 
 ['paco', '2/1/2018', '2.11', 'Critico'], 
 ['pepe', '2/1/2018', '11.2', 'Critico']]