toy list processing problem: collect similar terms

Xah Lee xahlee at gmail.com
Thu Oct 14 23:37:03 EDT 2010


On Sep 25, 9:05 pm, Xah Lee <xah... at gmail.com> wrote:
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((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))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
> ...

thanks all for many interesting solutions. I've been so busy in past
month on other computing issues and writing and never got around to
look at this thread. I think eventually i will, but for now just made
a link on my page to point to here.

now we have solutions in perl, python, ruby, common lisp, scheme lisp,
mathematica. I myself would also be interested in javascript perhps
i'll write one soon. If someone would go thru all these solution and
make a good summary with consistent format/names of each solution...
that'd be very useful i think. (and will learn a lot, which is how i
find this interesting)

PS here's a good site that does very useful comparisons for those
learning multiple langs.

    * 〈Lisp: Common Lisp, Scheme, Clojure, Emacs Lisp〉
http://hyperpolyglot.wikidot.com/lisp
    * 〈Scripting Languages: PHP, Perl, Python, Ruby, Smalltalk〉
http://hyperpolyglot.wikidot.com/scripting
    * 〈Scripting Languages: Bash, Tcl, Lua, JavaScript, Io〉
http://hyperpolyglot.wikidot.com/small
    * 〈Platform Languages: C, C++, Objective C, Java, C#〉
http://hyperpolyglot.wikidot.com/c
    * 〈ML: Standard ML, OCaml, F#, Scala, Haskell〉 http://hyperpolyglot.wikidot.com/ml

 Xah ∑ http://xahlee.org/


More information about the Python-list mailing list