Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > yet another list comprehension question

Reply
Thread Tools

yet another list comprehension question

 
 
Ross
Guest
Posts: n/a
 
      05-03-2009
I'm trying to set up a simple filter using a list comprehension. If I
have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
and I wanted to filter out all tuples containing None, I would like to
get the new list b = [(1,2), (3,4),(6,7)].

I tried b = [i for i in a if t for t in i is not None] but I get the
error that "t is not defined". What am I doing wrong?
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      05-03-2009
On Sat, May 2, 2009 at 7:13 PM, Ross <(E-Mail Removed)> wrote:
> I'm trying to set up a simple filter using a list comprehension. If I
> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
> and I wanted to filter out all tuples containing None, I would like to
> get the new list b = [(1,2), (3,4),(6,7)].


b = [tup for tup in a if None not in tup]

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Ross
Guest
Posts: n/a
 
      05-03-2009
On May 2, 7:21*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Sat, May 2, 2009 at 7:13 PM, Ross <(E-Mail Removed)> wrote:
> > I'm trying to set up a simple filter using a list comprehension. If I
> > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
> > and I wanted to filter out all tuples containing None, I would like to
> > get the new list b = [(1,2), (3,4),(6,7)].

>
> b = [tup for tup in a if None not in tup]
>
> Cheers,
> Chris
> --http://blog.rebertia.com


Thanks I feel retarded sometimes.
 
Reply With Quote
 
CTO
Guest
Posts: n/a
 
      05-03-2009
On May 2, 10:13*pm, Ross <(E-Mail Removed)> wrote:
> I'm trying to set up a simple filter using a list comprehension. If I
> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
> and I wanted to filter out all tuples containing None, I would like to
> get the new list b = [(1,2), (3,4),(6,7)].


try this:

b = [i for i in a if None not in i]

> I tried b = [i for i in a if t for t in i is not None] * but I get the
> error that "t is not defined". What am I doing wrong?


You've got a "for" and an "if" backwards. t isn't defined when the if
tries to evaluate it.



 
Reply With Quote
 
Snorri H
Guest
Posts: n/a
 
      05-04-2009
On May 3, 6:13 am, Ross <(E-Mail Removed)> wrote:
> I'm trying to set up a simple filter using a list comprehension. If I
> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
> and I wanted to filter out all tuples containing None, I would like to
> get the new list b = [(1,2), (3,4),(6,7)].
>
> I tried b = [i for i in a if t for t in i is not None] but I get the
> error that "t is not defined". What am I doing wrong?



Works as well:
filter(lambda x:not None in x, your_list)
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      05-04-2009
Snorri H <(E-Mail Removed)> writes:

> On May 3, 6:13 am, Ross <(E-Mail Removed)> wrote:
>> I'm trying to set up a simple filter using a list comprehension. If I
>> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
>> and I wanted to filter out all tuples containing None, I would like to
>> get the new list b = [(1,2), (3,4),(6,7)].
>>
>> I tried b = [i for i in a if t for t in i is not None] but I get the
>> error that "t is not defined". What am I doing wrong?

>
>
> Works as well:
> filter(lambda x:not None in x, your_list)

^^^^^^^^^^^^^
This is usually spelt 'None not in x'.

--
Arnaud

 
Reply With Quote
 
David Robinow
Guest
Posts: n/a
 
      05-04-2009
On Mon, May 4, 2009 at 2:33 AM, namekuseijin
<(E-Mail Removed)> wrote:
>>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)]
>>>> [(x,y) for (x,y) in ls if y]

> [(1, 2), (3, 4), (6, 7)]


Nope. That filters out 0 as well as None. Not what the OP asked for.
 
Reply With Quote
 
namekuseijin
Guest
Posts: n/a
 
      05-04-2009
On May 4, 9:15*am, David Robinow <(E-Mail Removed)> wrote:
> On Mon, May 4, 2009 at 2:33 AM, namekuseijin
>
> <(E-Mail Removed)> wrote:
> >>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)]
> >>>> [(x,y) for (x,y) in ls if y]

> > [(1, 2), (3, 4), (6, 7)]

>
> Nope. That filters out 0 as well as None. Not what the OP asked for.


True. I'm still a C programmer at heart I guess. ah, the flexibility
of 0...
 
Reply With Quote
 
namekuseijin
Guest
Posts: n/a
 
      05-06-2009
2009/5/5 Ricardo ArŠoz <(E-Mail Removed)>:
> This seems to work for any length tuples :
>
>>>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None),
>>>> (9, 0)]
>>>> [tup for tup in a if not [e for e in tup if e == None]]

> [(1, 2), (3, 4, 'goes'), (9, 0)]


Why that extra "for"? KISS

>>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), (9, 0)]

[(1, 2), (3, 4, 'goes'), (5, None), (6, 7, 8, 'as', None), (8, None), (9, 0)]
>>> [t for t in a if None not in t]

[(1, 2), (3, 4, 'goes'), (9, 0)]

"in" works perfectly well for any sequence, including strings.
 
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
List comprehension in if clause of another list comprehension Vedran Furac( Python 4 12-19-2008 01:35 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
List comprehension returning subclassed list type? Shane Geiger Python 4 03-25-2007 09:34 AM
Yet another book recommendation, but for someone who can program and yet does not the terminology well Berehem C Programming 4 04-28-2005 05:25 PM
List Comprehension question Mark Elston Python 1 12-11-2003 03:54 AM



Advertisments