我有一個匹配python列表的問題。匹配列表並將結果寫入新列表
list1 = [["id1","string1","string2"],["id2","string3","string4"]]
list2 = [["id1","string1","string2", "string3"],["id3","string4","string5", "string6"]]
我想是這樣的
list3 = [["id1", "string1", "string2", "string3"],["id2","string3","string4"],["id3","string4","string5", "string6"]]
如果從列表1的ID在列表2然後寫從列表2(例如["id1","string1","string2"]
)到一個新的列表中的元素。如果它不在列表中,則從list1中獲取元素並將其寫入新列表。在最終的結果應該是這個樣子
我嘗試了這種方式
for p in list1:
for d in list2:
if ((p[0] in list2)):
list3.append(d)
next
else:
list3.append(p)
到底是什麼原因呢? – msvalkon
請證明您至少已完成一些代碼並顯示您在哪裏掙扎 – sshashank124
輸出中元素的順序是否重要? – thefourtheye