[Tutor] how to delete some quasi-duplicated keys
lina
lina.lastname at gmail.com
Fri Nov 25 10:24:31 CET 2011
On Fri, Nov 25, 2011 at 5:06 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> lina wrote:
>>>>>
>>>>> 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:
>
>
> Which one do you want to keep?
>
It does not matter, the order is not important,
> If the order ('66', '69') is unimportant, you should use a frozenset instead
> of a tuple. In your code, where you set the pairs in the first place,
> instead of doing:
>
> pair = ('66', '69') # however you build the pair
> pairs[pair] = value
>
> (or how ever you set the initial values), change it to this:
>
> pair = frozenset(('66', '69'))
> pairs[pair] = pairs.get(pair, 0) + value
I don't get this "pairs.get" part.
pairs[pair]=pairs.get(pair,0)+parts[2]
TypeError: unsupported operand type(s) for +: 'int' and 'str'
or line in f.readlines():
parts=line.split()
#pair=set((parts[0],parts[1]))
if (parts[0],parts[1]) not in dehydrons.keys():
dehydrons[(parts[0],parts[1])]=parts[2]
occurence[(parts[0],parts[1])]=1
pair=frozenset(('parts[0]','parts[1]'))
pairs[pair]=pairs.get(pair,0)+parts[2]
else:
occurence[(parts[0],parts[1])]+=1
I upload the file in:
https://docs.google.com/open?id=0B93SVRfpVVg3NTg5YTkwMGUtMzdiNi00ZGI2LWE5ZGYtMTFmMTc0OTZhMzZl
https://docs.google.com/open?id=0B93SVRfpVVg3NGQwZDRhNDMtMDUzZi00NDIxLWE2MDAtZGM0ZWEzZDczYTMz
>
>
>
>
> --
> Steven
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list