dict generator question

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Sep 19 15:01:26 CEST 2008


Gerard flanagan:

> data.sort()
> datadict = \
> dict((k, len(list(g))) for k,g in groupby(data, lambda s:
>      '.'.join(s.split('.',2)[:2])))

That code may run correctly, but it's quite unreadable, while good
Python programmers value high readability. So the right thing to do is
to split that line into parts, giving meaningful names, and maybe even
add comments.

len(list(g))) looks like a good job for my little leniter() function
(or better just an extension to the semantics of len) that time ago
some people here have judged as useless, while I use it often in both
Python and D ;-)

Bye,
bearophile



More information about the Python-list mailing list