簡體   English   中英

python在字典列表中查找重復項並合並

[英]python find duplicates in list of dictionaries and merge

我正在使用python 2.7,我的問題與python詞典列表基於值查找重復項相同,除了我的唯一標識符基於多個鍵(id,名稱,地址等)之外

而且,基於這些鍵,我必須合並其他一些值

有什么建議么??

謝謝

因此,在一點幫助下,我獲得了這段代碼

uq_map = {}
for rec in outputs:
    #Set values that are marked as unique identifiers
    key = rec["o_date"], rec["o_hour"], rec["co_name"], rec["o_student"], rec["o_class"], rec["o_day"]
    #If they exist we append them to a new defined key 
    if key in uq_map:
        item = uq_map[key]
        print "item ",item
        item['o_teacher_set'].append(rec["o_teacher"])
        item['o_location_set'].append(rec["o_location"])            
    #if not we insert them into new key            
    else:
        item = rec.copy()
        item['o_teacher_set'] = [rec["o_teacher"]]
        item['o_location_set'] = [rec["o_location"]]
        uq_map[key] = item


print uq_map
#This is the loop to remove duplicates from nwe keys
for rec in uq_map.values():
    print 'Teachers: ', '+'.join(set(rec['o_teacher_set']))

如果還有更多Python解決方案,請讓我知道

謝謝您最好的問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM