[Tutor] how to delete some quasi-duplicated keys

Steven D'Aprano steve at pearwood.info
Fri Nov 25 12:19:28 CET 2011


lina wrote:
> On Fri, Nov 25, 2011 at 5:06 PM, Steven D'Aprano <steve at pearwood.info> wrote:

>> pair = frozenset(('66', '69'))
>> pairs[pair] = pairs.get(pair, 0) + value
> 
> I don't get this "pairs.get" part.

The "get" method does a look-up on a dict, but instead of failing if the 
key is missing, it returns a default value:

py> d = {'a': 2}
py> d['b']  # fails
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
KeyError: 'b'
py> d.get('b', 3)  # use 3 as the default
3


>  pairs[pair]=pairs.get(pair,0)+parts[2]
> TypeError: unsupported operand type(s) for +: 'int' and 'str'

Lina, in your original example, the values of the dict are integers:

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

The error you show above can only happen if they are not integers, but 
strings. When you show us examples, please get the examples right. If 
you give us wrong information, how do you expect us to help?

You should convert all the strings into ints.


> 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


f = open("some file")
dehydrons = {}
occurrence = {}
pairs = {}
for line in f.readlines():
     parts = line.split()
     # convert to ints
     parts = [int(s) for s in parts]
     pair = frozenset(parts[:2])  # order doesn't matter
     if pair in dehydrons:
         occurrence[pair] += 1
     else:
         dehydrons[pair] = parts[2]
         occurrence[pair] = 1
         pairs[pair] = pairs.get(pair, 0) + parts[2]
f.close()



-- 
Steven


More information about the Tutor mailing list