[melbourne-pug] Fun with phononyms

Tennessee Leeuwenburg tleeuwenburg at gmail.com
Mon Aug 17 06:50:36 CEST 2009


Hi Richard,

I've always used such things with some caution. I work with a lot of people
who are probably not aware of many of the libraries in Python. For that
reason, I try to stick to the standard dict. Calling setdefault is a good
hint to a Python newbie that something particular is 'going on' and that
they should look it up. However, if you pass in an object, it might not be
clear that something is a defaultdict rather than a regular dict, and it
could be easy to lose track of when you are allowed to use defaultdict
functionality.

It seems a bit kinder to me on new programmers and team members not to use
defaultdict.

I think ideally I'd like the standard dict to accept another argument, i.e.

myDict["fruit", []].append(banana)

Cheers,
-T

On Mon, Aug 17, 2009 at 2:30 PM, Richard Baron Penman
<richardbp at gmail.com>wrote:

> > 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
>>
>>
>
> _______________________________________________
> 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"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20090817/07e248c1/attachment.htm>


More information about the melbourne-pug mailing list