[Tutor] how to delete some quasi-duplicated keys

lina lina.lastname at gmail.com
Fri Nov 25 09:41:52 CET 2011


>>> pairs
{('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25}


such as here ('66', '69') and ('69', '66') is one key,

I wanna keep only one and add the value of those two keys, above is a
very simple example:

here is the (failed) code:

        for k, v in pairs.items():
            if str(k)[1]+str(k)[0] in pairs.keys():
                print(pairs[str(k)[1]+str(k)[0]])
                pairs[k]+=pairs[str(k)[1]+str(k)[0]]
                del pairs[str(k)[1]+str(k)[0]]
            print(v,k)


Thanks for any advice,


More information about the Tutor mailing list