0

I want to filter data based on items in drop.

data = [
    ['Basket', 'NBA ET', 'Kobe'],
    ['Basket', 'NCAA', 'Shaq'],
    ['Basket', 'ENG', 'Shaq'],
]
drop = ['NBA', 'NCAA']

And because I want the list with NBA ET to be left out too, it must be something beyond:

filtered = [d for d in data if d[1] not in drop]   # assume d[1] will hold

What I need is:

# pseudocode
filtered = [d for d in data if _ not in d[1] for _ in drop]

but I can never remember the syntax.

For the record, filtered should results in [['Basket', 'ENG', 'Shaq']]

2 Answers 2

2

You can use any() and split the string on whitespace:

filtered = [d for d in data if not any(dropped in d[1].split() for dropped in drop)]

If you make drop a set, just test for an intersection:

drop = set(drop)

filtered = [d for d in data if not drop.intersection(d[1].split())]

The latter should be more performant the larger drop becomes.

Demo:

>>> data = [
...     ['Basket', 'NBA ET', 'Kobe'],
...     ['Basket', 'NCAA', 'Shaq'],
...     ['Basket', 'ENG', 'Shaq'],
... ]
>>> drop = ['NBA', 'NCAA']
>>> [d for d in data if not any(dropped in d[1].split() for dropped in drop)]
[['Basket', 'ENG', 'Shaq']]
>>> drop = set(drop)
>>> [d for d in data if not drop.intersection(d[1].split())]
[['Basket', 'ENG', 'Shaq']]
Sign up to request clarification or add additional context in comments.

2 Comments

@nutship: ick, yes, you want to remove not include. Corrected.
@nutship: I wasn't downvoted; someone removed their upvote instead.
1
[row for row in data if not any(league in row[1].split() for league in drop)]
# [['Basket', 'ENG', 'Shaq']]

6 Comments

There is no need to create a list for any(). In fact, that defeats the purpose of using any() in the first place. Don't use a list comprehension here, use a generator expression instead (drop the [...] brackets from the any() call).
Strange, when I remove the list comprehension, the result is empty. What is the reason for this?
No, your code works for me. Your code is identical to mine without the [..] (save for the names used).
I'm on python 2.7, could that be the reason?
No, I use Python 2.7 too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.