Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > do you master list comprehensions?

Reply
Thread Tools

do you master list comprehensions?

 
 
Will Stuyvesant
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
 
 
 
Max M
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-13-2004
>>> 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-13-2004
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>



 
Reply With Quote
 
Max M
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
Luis M. Gonzalez
Guest
Posts: n/a
 
      12-14-2004
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']
>>>


 
Reply With Quote
 
Luis M. Gonzalez
Guest
Posts: n/a
 
      12-14-2004
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']
>>>


 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      12-14-2004
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2nd Post: VB Master-Details Edit (Separate Pages), Response Redirect to Master Page Databinding Issue... Sam ASP .Net 1 07-20-2006 06:17 PM
VB Master-Details Edit (Separate Pages), Response Redirect to Master Page Databinding Issue... Sam ASP .Net 0 07-20-2006 02:31 AM
Cross Page Post Back When Using Mixture of Master Pages and Non-Master Pages Jason@webbit.com ASP .Net 1 05-16-2006 02:00 PM
Master Pages Great! But what about Master User Controls? Electrified Research ASP .Net 5 10-11-2005 02:32 PM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM



Advertisments