0

我正在使用 python 2.7 我的问题与python 字典列表基于值查找重复项相同, 除了我的唯一标识符基于多个键(id、名称、地址等)

而且,基于这些键,我必须合并一些其他值

有什么建议么??

谢谢你

4

1 回答 1

1

所以在一些帮助下我得到了这段代码

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']))

如果还有更多 pythonic 解决方案,请告诉我

谢谢您最好的问候

于 2013-07-09T12:00:57.583 回答