Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
RE: List Combinations
Reedick, Andrew
3/12/2008 3:38:00 PM
> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Shane Geiger
> Sent: Wednesday, March 12, 2008 10:33 AM
> To: Michael Wieher
> Cc: python-list@python.org
> Subject: Re: List Combinations
>
> >
>
>
> def gen(lists):
> out = '[' + ','.join(["v%s" % i for i in range(len(lists))]) +
']'
> comp = ''.join([ " for v%d in lists[%d]" % (i, i) for i in
> range(len(lists))])
> return eval('[ ' + out + comp + ' ]')
>
> a,b,c = [1,2,3],[4,5,6],[7,8,9]
>
> print gen([a, b, c])
>
>
>
>
>
> --
> Shane Geiger
> IT Director
> National Council on Economic Education
> sgeiger@ncee.net | 402-438-8958 |
http://ww...
>
It helps to quote your source...
http://www.mail-archive.com/python-list@python.org/msg1...
Start here
http://www.mail-archive.com/python-list@python.org/msg1...
and go through the thread. There are several ways to solve the problem
and we evaluated the performance and 'pythonicity' of each.
*****
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625
1 Answer
Arnaud Delobelle
3/12/2008 10:55:00 PM
0
On Mar 12, 3:38 pm, "Reedick, Andrew" <jr9...@ATT.COM> wrote:
[...]
> Start here
>
>
http://www.mail-archive.com/python-l...@python.org/msg1...
> and go through the thread. There are several ways to solve the problem
> and we evaluated the performance and 'pythonicity' of each.
I used a kind of extended cartesian product function a while ago while
writing a parser. If I simplify it down to a simple product function
it goes like this:
def product(*sequences):
i, n = 0, len(sequences)
vals = [None]*n
iters = map(iter, sequences)
while i >= 0:
if i == n:
yield tuple(vals)
i -= 1
else:
for vals[i] in iters[i]:
i += 1
break
else:
iters[i] = iter(sequences[i])
i -= 1
It's neither recursive nor a hack, I haven't tried to measure it
against other approaches (obviously it wouldn't beat the eval(...)
hack). I haven't optimised it for readability (by others) either :)
--
Arnaud
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
RE: List Combinations
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password