DaveM wrote:
On Sun, 27 Jul 2008 16:57:14 +0200, "Diez B. Roggisch" <[EMAIL PROTECTED]>
wrote:

Marc 'BlackJack' Rintsch schrieb:
On Sun, 27 Jul 2008 16:41:19 +0200, Diez B. Roggisch wrote:

DaveM schrieb:
Getting back to the list concatenation, I finally found the itertools.chain command which is the most compact and fastest (or second fastest by a trivial amount, I can't remember which). Along the way, I must have tried/used half a dozen methods, ...which brings me back my initial PERL comment. There's more than one way to do it in Python, too.

But I *do* know that taking the python zen literally is fruitless.

I think it should be taken more literally than the wrong reduction to "there should be only one way". People tend to forget "obvious" and "preferably" all the time.

Good point. The OP found the obvious way of extending. I wonder what his reasons were to abandon it.

You'll have guessed, I'm sure, that I'm not a professional programmer. This
was the third rewrite of a program to match candidate groups to examiners on
a three day course I run, necessitated on this occasion by a change in the
structure of the course. I originally learnt python as I wrote, and some of
the early code was ugly and verbose, so once the current rewrite was working
I took the opportunity to tidy the code up and document it (yes, I know, but
as I said, I'm an amateur). The list concatenation was an itch I couldn't
scratch:

    temp = []
    for value in sessexam.values():
        temp.extend(value)
    c_exam = [name for name in set(temp)] #See what I mean about verbose?
    c_exam.sort()
    return c_exam

Six lines just didn't feel like it ought to be the best way to do something
so simple. I liked the attempt below better, but was foolish enough to time
it, so that was the end of that.

    return sorted(list(set(reduce(lambda x, y: x+y, sessexam.values()))))

The current version (below) is a compromise, but I still feel there _ought_
to be a simple one word command to join multiple lists.

    a = list(set(itertools.chain(*sessexam.values())))
    a.sort() #As I write I'm wondering if I really need it sorted. Hmm...
    return a

Didn't someone already answer that. List addition and sum() both do what you want.

>>> A = [1,2,3]
>>> B = [4,5,6]
>>> C = [7,8,9]

>>> A+B+C
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> sum([A,B,C], [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

It doesn't get any easier than that.

Gary Herron



DaveM
--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to