[Tutor] how to delete some quasi-duplicated keys
lina
lina.lastname at gmail.com
Fri Nov 25 13:40:39 CET 2011
On Fri, Nov 25, 2011 at 7:19 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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:
Ha ... it's me.
>
> {('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?
Sorry, I was confused at that time.
>
> 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()
>
for line in f.readlines():
parts = line.split()
#pair=set((parts[0],parts[1]))
#convert to ints
parts = [int(s) for s in parts]
pair = frozenset(parts[:2])
print(pair)
if pair in dehydrons:
occurence[pair] += 1
else:
dehydrons[pair] = parts[2]
pairs[pair] = pairs.get(pair,0) + parts[2]
print(pairs)
$ python3 dehydron_data_frozenset_version.py
frozenset({2, 15})
frozenset({2, 15})
Traceback (most recent call last):
File "dehydron_data_frozenset_version.py", line 35, in <module>
occurence[pair] += 1
KeyError: frozenset({2, 15})
Thanks,
>
>
> --
> 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