Supposeand i want this outputCode:a={1: 'url.txt', 2: x, 3: x, 4: z, 5: 'fb.txt''}
so i basically want to remove duplicate values ; but how ?Code:a={1:'url.txt',2:x,3:'fb.txt'}
Supposeand i want this outputCode:a={1: 'url.txt', 2: x, 3: x, 4: z, 5: 'fb.txt''}
so i basically want to remove duplicate values ; but how ?Code:a={1:'url.txt',2:x,3:'fb.txt'}
You could invert the dict..
something like:
This is to also maintain the first key for a duplicated value -- otherwise, you can skip the "has_key()" check and it'll store only the last key of the duplicated value.Code:inverted = dict() for (k, v) in a.iteritems(): if not inverted.has_key(v): inverted[v] = k
To get back to original dict, you just need to invert this dict again - like:
If you don't want to preserve the keys from original dict at all but just want integers as key, you could simply do:Code:new_dict = dict() for (k, v) in inverted.iteritems(): new_dict[v] = k
HTHCode:new_dict = dict() i = 1 for v in inverted.iterkeys(): new_dict[i] = v i += 1![]()
The Unforgiven
thank you so much![]()
Bookmarks