toy list processing problem: collect similar terms

Xah Lee xahlee at
Sun Sep 26 22:56:52 CEST 2010


On Sep 25, 11:17 pm, Paul Rubin <no.em... at nospam.invalid> wrote:
> Python solution follows (earlier one with an error cancelled).  All
> crossposting removed since crossposting is a standard trolling tactic.
>     from collections import defaultdict
>     def collect(xss):
>         d = defaultdict(list)
>         for xs in xss:
>             d[xs[0]].extend(xs[1:])
>         return list(v for k,v in sorted(d.items()))
>     y = [[0,'a','b'], [1,'c','d'], [2,'e','f'], [3,'g','h'], [1,'i','j'],
>          [2,'k','l'], [4,'m','n'], [2,'o','p'], [4,'q','r'], [5,'s','t']]
>     print collect(y)

Hi Paul,

thanks for your solution, and thanks to many other who provided
solutions. It'll take some time to go thru them.

btw, i disagree about your remark on crossposting. For those
interested, you can read in the following:

• 〈Cross-posting & Language Factions〉

if anyone wants to argue with me about this, there's a my blog link at
the bottom of the page where you can leave a comment. Feel free to use

i'll go over the solutions and post if i have anything interesting to
say. ☺ Possbly will select some to show on my site with credit of

 Xah ∑ ☄

More information about the Python-list mailing list