[英]How to merge two lists of arrays, (x,y,z,m), excluding duplicates based on only (x,y,z)
我有兩個表格形式
list1 = list(zip(SGXm, SGYm, SGZm, Lm))
list2 = list(zip(SGXmm, SGYmm, SGZmm, Lmm))
我想合並它們,同時排除重復的(x,y,z)條目,並忽略L中的差異。
list1.extend(x for x in list2 if x not in list1)
僅對我的x,y,z執行此工作,但是我想保留L(在可以選擇的情況下,在第一個列表中)。
您必須提取需要進行比較的三元組。
seen = set(item[:3] for item in list1)
list1.extend(item for item in list2 if item[:3] not in seen)
如果您想對輸出進行排序(特別是如果您已經對輸入進行排序),則itertools.groupby
和heapq.merge
很好地結合使用。 如果輸入尚未排序,則需要這樣做。 可以一次連接並排序所有內容:
from operator import itemgetter
commonkey = itemgetter(0, 1, 2)
combined = sorted(list1 + list2, key=commonkey)
或者,如果它們已經被排序,或者您想獨立地排序,請使用heapq.merge
並避免對輸入內容進行淺表復制:
# Explicit sort calls only necessary if inputs not already sorted
list1.sort(key=commonkey)
list2.sort(key=commonkey)
# Combine already sorted inputs with heapq.merge, avoiding intermediate lists
combined = heapq.merge(list1, list2, key=commonkey)
無論選擇哪種方法,都可以通過對groupby
的簡單理解來跟進它,只需獲取每個唯一組中的第一個條目,就僅保留每個唯一鍵的一個副本:
# Groups neighboring entries with the same key, and we keep only the first one
uniq = [next(g) for _, g in itertools.groupby(combined, key=commonkey)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.