0

I am using python 2.7 my question is the same as python list of dictionaries find duplicates based on value except I my unique identifier is based on multiple keys (id, name, address,etc.)

And also, based on those keys I have to merge some other values

Any suggestions??

thank you

Community
  • 1
  • 1
Yebach
  • 1,661
  • 8
  • 31
  • 58

1 Answers1

1

So with a little help I got to this piece of code

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

if there are any more pythonic solutons please let me know

thank you best regards

Yebach
  • 1,661
  • 8
  • 31
  • 58