[melbourne-pug] Fun with phononyms

Richard Baron Penman richardbp at gmail.com
Mon Aug 17 06:30:09 CEST 2009


> I always wish setdefault looked a little nicer, but it's such a great
defensive programming technique that I just use it anyway.

instead of setdefault you can often use defaultdict:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d['fruit'].append('apple')
>>> d['fruit'].append('banana')
>>> print d['fruit']
['apple', 'banana']

Richard



On Mon, Aug 17, 2009 at 1:15 PM, Tennessee Leeuwenburg <
tleeuwenburg at gmail.com> wrote:

> Cool, I didn't know about maketrans. Thanks.
>
> I always wish setdefault looked a little nicer, but it's such a great
> defensive programming technique that I just use it anyway.
>
> Thanks for kicking the discussion off, M. Nice to see some chatter on the
> list at last! :)
>
> Cheers,
> -T
>
>
>
> On Mon, Aug 17, 2009 at 1:09 PM, John La Rooy <john.larooy at gmail.com>wrote:
>
>> This is how I would do it.
>>
>> #!/usr/bin/env python
>> from string import maketrans
>>
>> dictionary = "sowpods.txt"
>> nokia =
>> maketrans("abcdefghijklmnopqrstuvwxyz","22233344455566677778889999")
>> phononyms = {}
>>
>> for word in open(dictionary):
>>     word = word.strip().lower()
>>    phononyms.setdefault(word.translate(nokia), []).append(word)
>>
>> for k,v in phononyms.iteritems():
>>    if len(v) > 1:
>>        print v
>> _______________________________________________
>> melbourne-pug mailing list
>> melbourne-pug at python.org
>> http://mail.python.org/mailman/listinfo/melbourne-pug
>>
>
>
>
> --
> --------------------------------------------------
> Tennessee Leeuwenburg
> http://myownhat.blogspot.com/
> "Don't believe everything you think"
>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20090817/583e3ffe/attachment-0001.htm>


More information about the melbourne-pug mailing list