REQ: Small Perl to Python conversion needed

John Machin sjmachin at lexicon.net
Thu Jun 2 20:51:23 EDT 2005


Koncept wrote:
> Howdie Python folks! I am very new to Python ( 3rd day now ) and it has
> already earned its place as my fav. language to work in.  I hope to
> continue, and I really would appreciate some good resources if anybody
> would care to contribute.
> 
> My current head-scratcher concerns something I can do in Perl which I
> would like to have duplicated for Python. I have noticed that it is not
> possible to increment an unset value in Python, so I would like to know
> how to duplicate the following bit of code using Python dictionaries.
> 

[expletives deleted]

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




More information about the Python-list mailing list