REQ: Small Perl to Python conversion needed

Steven Bethard steven.bethard at gmail.com
Thu Jun 2 21:10:45 EDT 2005


John Machin wrote:
> freq_dict = {}
> ...
> if thing in freq_dict:
>     freq_dict[thing] += 1
> else:
>     freq_dict[thing] = 1
> 
> or, less plainly,
> 
> freq_dict[thing] = freq_dict.get(thing, 0) + 1

or

try:
     freq_dict[thing] += 1
except KeyError:
     freq_dict[thing] = 1

STeVe



More information about the Python-list mailing list