Re: convert loop to list comprehension

2006-09-09 Thread [EMAIL PROTECTED]
Paul Rubin wrote: > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > > FWIW, the original loop looked perfectly fine and readable and I'd > > > suggest going with that over these hacked-up listcomp solutions. Don't > > > use a listcomp just for the sake of using a listcomp. > > > > Thanks for

Re: convert loop to list comprehension

2006-09-09 Thread Steve Holden
[EMAIL PROTECTED] wrote: > Carl Banks wrote: > >>[EMAIL PROTECTED] wrote: >> >>>Paul Rubin wrote: >>> [EMAIL PROTECTED] writes: >print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm as

Re: convert loop to list comprehension

2006-09-09 Thread Steve Holden
[EMAIL PROTECTED] wrote: [...] > > Cool ... and damn but you guys are fast with the answers. This appears > to work find, but in a quick and dirty test it appears that the [list] > version takes about 2x as long to run as the original loop. Is this > normal? > No hard and fast information, but as

Re: convert loop to list comprehension

2006-09-09 Thread Simon Forman
[EMAIL PROTECTED] wrote: > Thanks for that, Carl. I think that using the loop is probably what > I'll end up doing. I had no idea that the listcomp thing would be quite > a complicated as it is appearing. I had it in my mind that I was > missing some obvious thing which would create a simple solut

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > FWIW, the original loop looked perfectly fine and readable and I'd > > suggest going with that over these hacked-up listcomp solutions. Don't > > use a listcomp just for the sake of using a listcomp. > > Thanks for that, Carl. I think that using

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Carl Banks wrote: > [EMAIL PROTECTED] wrote: > > Paul Rubin wrote: > > > [EMAIL PROTECTED] writes: > > > > print sum( ([i]*n for i,n in enumerate(seq)), []) > > > > > > Wow, I had no idea you could do that. After all the discussion about > > > summing strings, I'm astonished. > > > > Me too ... d

Re: convert loop to list comprehension

2006-09-08 Thread Carl Banks
[EMAIL PROTECTED] wrote: > Paul Rubin wrote: > > [EMAIL PROTECTED] writes: > > > print sum( ([i]*n for i,n in enumerate(seq)), []) > > > > Wow, I had no idea you could do that. After all the discussion about > > summing strings, I'm astonished. > > Me too ... despite what bearophile said, this is

Re: convert loop to list comprehension

2006-09-08 Thread MonkeeSage
Paul Rubin wrote: > "MonkeeSage" <[EMAIL PROTECTED]> writes: > > Ps. I don't know if xrange is faster...I thought the difference was > > that range created a temporary variable holding a range object and > > xrange created an iterator? > > There's no such thing as a "range object"; range creates a

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
"MonkeeSage" <[EMAIL PROTECTED]> writes: > Ps. I don't know if xrange is faster...I thought the difference was > that range created a temporary variable holding a range object and > xrange created an iterator? There's no such thing as a "range object"; range creates a list, which consumes O(n) mem

Re: convert loop to list comprehension

2006-09-08 Thread MonkeeSage
[EMAIL PROTECTED] wrote: > Cool ... and damn but you guys are fast with the answers. This appears > to work find, but in a quick and dirty test it appears that the [list] > version takes about 2x as long to run as the original loop. Is this > normal? You could also do it 'functionally' with map(),

Re: convert loop to list comprehension

2006-09-08 Thread Felipe Almeida Lessa
08 Sep 2006 17:33:20 -0700, Paul Rubin <"http://phr.cx"@nospam.invalid>: > [EMAIL PROTECTED] writes: > > print sum( ([i]*n for i,n in enumerate(seq)), []) > > Wow, I had no idea you could do that. After all the discussion about > summing strings, I'm astonished. Why? You already had the answer: s

Re: convert loop to list comprehension

2006-09-08 Thread Felipe Almeida Lessa
8 Sep 2006 17:37:02 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > 1. Using an _ is an interesting way to use a throw-away variable. Never > would I think of that ... but, then, I don't do Perl either :) It's a kind of convention. For example, Pylint complains for all variables you set and don't

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Paul Rubin wrote: > [EMAIL PROTECTED] writes: > > print sum( ([i]*n for i,n in enumerate(seq)), []) > > Wow, I had no idea you could do that. After all the discussion about > summing strings, I'm astonished. Me too ... despite what bearophile said, this is faster than the 2nd example. Nearly as f

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote: > Two possibile solutions: > > seq = [2, 3, 1, 9] > > print sum( ([i]*n for i,n in enumerate(seq)), []) > > print [i for i, x in enumerate(seq) for _ in xrange(x)] > Cool as well. So much to learn :) 1. Using an _ is an interesting way to use a throw-away variable. Never

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
[EMAIL PROTECTED] writes: > print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. -- http://mail.python.org/mailman/listinfo/python-list

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Rob Williscroft wrote: > But my forth attemp yeilded (If that's a pun I do appologise) > this: > > >>> [ x for a in range(len(seq)) for x in [a] * seq[a] ] Ahh, that's the magic ... I didn't understand that one could have multiple "statments" in this single line. Now, you can't have python line

Re: convert loop to list comprehension

2006-09-08 Thread bearophileHUGS
Two possibile solutions: seq = [2, 3, 1, 9] print sum( ([i]*n for i,n in enumerate(seq)), []) print [i for i, x in enumerate(seq) for _ in xrange(x)] The second one is probably quite faster. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: convert loop to list comprehension

2006-09-08 Thread Rob Williscroft
[EMAIL PROTECTED] wrote in news:1157758817.446690.105620 @i42g2000cwa.googlegroups.com in comp.lang.python: > Please help my poor brain :) Every time I try to do a list > comprehension I find I just don't comprehend ... > > Anyway, I have the following bit of code: > > seq = [2, 3, 1, 9] > tmp =

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
Paul Rubin writes: > > Question is, can I do this as a list comprehension? > > import operator > x = reduce(operator.add, ([i]*a for i,a in enumerate(seq)), []) Maybe more in the iterative spirit: import itertools seq = [2, 3, 1, 9] x = itertools.chain(*([i]*a for i

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > seq = [2, 3, 1, 9] > tmp = [] > for a in range(len(seq)): > tmp.extend([a]*seq[a]) > > which correctly returns: > > [0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3] > > Question is, can I do this as a list comprehension? import operator x =

convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Please help my poor brain :) Every time I try to do a list comprehension I find I just don't comprehend ... Anyway, I have the following bit of code: seq = [2, 3, 1, 9] tmp = [] for a in range(len(seq)): tmp.extend([a]*seq[a]) which correctly returns: [0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3