Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   do you master list comprehensions? (http://www.velocityreviews.com/forums/t339194-do-you-master-list-comprehensions.html)

Will Stuyvesant 12-13-2004 08:51 PM

do you master list comprehensions?
 
Here is a question about list comprehensions [lc]. The
question is dumb because I can do without [lc]; but I am
posing the question because I am curious.

This:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> result = []
>>> for d in data:

.... for w in d:
.... result.append(w)
>>> print result

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

puts all the words in a list, like I want.

How to do this with [lc] instead of for-loops?

I tried funnies like [[w for w in L] for L in data],
that is correct syntax, but you'd never guess.

I know, silly! No need for [lc]! So there's my
question. I am sure a one-liner using [lc] will be very
enlightening. Like studying LISP.


--
I wish there was a knob on the TV to turn up the intelligence.
There's a knob called `brightness', but it doesn't work.
-- Gallagher


Max M 12-13-2004 09:07 PM

Re: do you master list comprehensions?
 
Will Stuyvesant wrote:

> I tried funnies like [[w for w in L] for L in data],
> that is correct syntax, but you'd never guess.


That is absolutely correct. It's not a funnie at all. If you find it odd
it's only because you are not used to list comprehensiones.

In that case you might be more comfortable with:

data = [['foo','bar','baz'],['my','your'],['holy','grail']]
result = []
for l in data:
result += l


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Peter Otten 12-13-2004 09:08 PM

Re: do you master list comprehensions?
 
Will Stuyvesant wrote:

>>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>>> result = []
>>>> for d in data:

> ... for w in d:
> ... result.append(w)
>>>> print result

> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
>
> puts all the words in a list, like I want.
>
> How to do this with [lc] instead of for-loops?


>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> [w for d in data for w in d]

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

See how the for expressions in the list comprehension exactly match your
nested for loops? That's all there is to it.

Peter


Steven Bethard 12-13-2004 09:09 PM

Re: do you master list comprehensions?
 
Will Stuyvesant wrote:
>>>>data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>>>result = []
>>>>for d in data:

>
> ... for w in d:
> ... result.append(w)
>
>>>>print result

>
> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
>


Take advantage of the fact that you can have more than one 'for' in a
list comprehension:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> [item for item_list in data for item in item_list]

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

Steve

Diez B. Roggisch 12-13-2004 09:20 PM

Re: do you master list comprehensions?
 
>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> [e for l in data for e in l]

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
--
Regards,

Diez B. Roggisch

Fredrik Lundh 12-13-2004 09:48 PM

Re: do you master list comprehensions?
 
Max M wrote:

>> I tried funnies like [[w for w in L] for L in data],
>> that is correct syntax, but you'd never guess.

>
> That is absolutely correct. It's not a funnie at all. If you find it odd it's only because you are
> not used to list comprehensiones.


well, syntactically correct or not, it doesn't do what he want...

> In that case you might be more comfortable with:
>
> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
> result = []
> for l in data:
> result += l


how about (slightly evil):

result = []; map(result.extend, data)

</F>




Max M 12-13-2004 11:41 PM

Re: do you master list comprehensions?
 
Fredrik Lundh wrote:
> Max M wrote:
>
>
>>>I tried funnies like [[w for w in L] for L in data],

>>
>>That is absolutely correct. It's not a funnie at all.

>
> well, syntactically correct or not, it doesn't do what he want...


Doh! *I* might not be used to list comprehensions then... You are right.

That example could have been expressed more clearly as:

result = data

;-)

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Luis M. Gonzalez 12-14-2004 01:44 AM

Re: do you master list comprehensions?
 
I guess the simplest to do it is like this:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> result=[w for d in data for w in d]
>>> result

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
>>>



Luis M. Gonzalez 12-14-2004 01:44 AM

Re: do you master list comprehensions?
 
I guess the simplest way to do it is like this:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> result=[w for d in data for w in d]
>>> result

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
>>>



James Stroud 12-14-2004 02:13 AM

Re: do you master list comprehensions?
 
Here is one for arbitrary depth:

def unroll(ary):
unrolled = []
for item in ary:
# add test for your favorite sequence type
if ( type(item) == types.ListType or \
type(item) == types.TupleType \
):
unrolled.extend(unroll(item))
else:
unrolled.append(item)
return unrolled



>>> unroll([[1, 2, 3], ('fred', 'barney', ['wilma', 'betty']), 'dino'])

[1, 2, 3, 'fred', 'barney', 'wilma', 'betty', 'dino']




On Monday 13 December 2004 12:51 pm, Will Stuyvesant wrote:
> Here is a question about list comprehensions [lc]. The
> question is dumb because I can do without [lc]; but I am
> posing the question because I am curious.
>
> This:
> >>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
> >>> result = []
> >>> for d in data:

>
> ... for w in d:
> ... result.append(w)
>
> >>> print result

>
> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
>
> puts all the words in a list, like I want.
>
> How to do this with [lc] instead of for-loops?
>
> I tried funnies like [[w for w in L] for L in data],
> that is correct syntax, but you'd never guess.
>
> I know, silly! No need for [lc]! So there's my
> question. I am sure a one-liner using [lc] will be very
> enlightening. Like studying LISP.


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
611 Charles E. Young Dr. S.
MBI 205, UCLA 951570
Los Angeles CA 90095-1570
http://www.jamesstroud.com/


All times are GMT. The time now is 09:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.