'Advanced' list comprehension? query

P

pyscottishguy

Hi,

I'm playing around with list comprehension, and I'm trying to find the
most aesthetic way to do the following:

I have two lists:

noShowList = ['one', 'two', 'three']

myList = ['item one', 'item four', 'three item']

I want to show all the items from 'myList' that do not contain any of
the strings in 'noShowList'.

i.e. 'item four'

I can do it like this:

def inItem(noShowList, listitem):
return [x for x in noShowList if x in listitem]

print [x for x in myList if not inItem(noShowList, x)]

and I can do it (horribly) with:

print [x for x in myList if not (lambda y, z:[i for i in y if i in z])
(noShowList, x)]

I can also print out the items that DO contain the 'noShowList'
strings with:

print [x for x in myList for y in noShowList if y in x]

but I can't get the 'not' bit to work in the above line.

Any ideas?
Thanks!
 
B

BartlebyScrivener

Hi,

I'm playing around with list comprehension, and I'm trying to find the
most aesthetic way to do the following:

I have two lists:

noShowList = ['one', 'two', 'three']

myList = ['item one', 'item four', 'three item']

I want to show all the items from 'myList' that do not contain any of
the strings in 'noShowList'.

I'm still learning these myself, but I think what you want is
itertools:

http://docs.python.org/lib/module-itertools.html
 
P

Peter Otten

I'm playing around with list comprehension, and I'm trying to find the
most aesthetic way to do the following:

I have two lists:

noShowList = ['one', 'two', 'three']

myList = ['item one', 'item four', 'three item']

I want to show all the items from 'myList' that do not contain any of
the strings in 'noShowList'.
no_show_list = ["one", "two", "three"]
my_list = ["item one", "item four", "three item"]
[item for item in my_list if not any(x in item for x in no_show_list)]
['item four']

Peter
 
D

danmcleran

Hi,

I'm playing around with list comprehension, and I'm trying to find the
most aesthetic way to do the following:

I have two lists:

noShowList = ['one', 'two', 'three']

myList = ['item one', 'item four', 'three item']

I want to show all the items from 'myList' that do not contain any of
the strings in 'noShowList'.

i.e. 'item four'

I can do it like this:

def inItem(noShowList, listitem):
return [x for x in noShowList if x in listitem]

print [x for x in myList if not inItem(noShowList, x)]

and I can do it (horribly) with:

print [x for x in myList if not (lambda y, z:[i for i in y if i in z])
(noShowList, x)]

I can also print out the items that DO contain the 'noShowList'
strings with:

print [x for x in myList for y in noShowList if y in x]

but I can't get the 'not' bit to work in the above line.

Any ideas?
Thanks!

import itertools

from itertools import ifilter

noShowList = ['one', 'two', 'three']
myList = ['item one', 'item four', 'three item']

result = list(ifilter(lambda x: True not in [item in x for item in
noShowList], myList))

print result
 
J

Jason

Hi,

I'm playing around with list comprehension, and I'm trying to find the
most aesthetic way to do the following:

I have two lists:

noShowList = ['one', 'two', 'three']

myList = ['item one', 'item four', 'three item']

I want to show all the items from 'myList' that do not contain any of
the strings in 'noShowList'.

i.e. 'item four'

I can do it like this:

def inItem(noShowList, listitem):
return [x for x in noShowList if x in listitem]

print [x for x in myList if not inItem(noShowList, x)]

and I can do it (horribly) with:

print [x for x in myList if not (lambda y, z:[i for i in y if i in z])
(noShowList, x)]

I can also print out the items that DO contain the 'noShowList'
strings with:

print [x for x in myList for y in noShowList if y in x]

but I can't get the 'not' bit to work in the above line.

Any ideas?
Thanks!

Here's how I would do it:
noShowList ['one', 'two', 'four']
myList ['item one', 'item two', 'item three', 'item four', 'item five']
[x for x in myList if not any(y in x for y in noShowList)] ['item three', 'item five']

--Jason
 
S

scottishguy

Thanks for some great explanations and alternatives!

I'll go with the any() approach, as its nice and readable (my
noShowList is quite small compared to myList). Thanks, John, for the
'make it obvious' tip.

As an exercise, if I now wanted to show the entries where the
'noShowList' matches, what would be the best option (using list
comprehension)

i.e. ['item one', 'item three']

My original version:
print [x for x in myList for y in noShowList if y in x]

Version based on previous answer:
print [x for x in myList if any(y in x for y in noShowList)]

Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top