4

I have two lists looking like:

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]

I want to substring filter list1 by elements of list2 and get expected output as follows:

outcome = ['bj-100-cy', 'sh-200-pd']

When doing:

list1 = str(list1)
list2 = str(list2)
outcome = [x for x in list2 if [y for y in list1 if x in y]]

I get a result like this: ['[', '1', '0', '0', ',', ' ', '2', '0', '0', ']']. How can I filter it correctly? Thanks.

Reference related:

Is it possible to filter list of substrings by another list of strings in Python?

7 Answers 7

8

List comprehension and any:

[i for i in list1 if any(i for j in list2 if str(j) in i)]

any to check if any element of list2 is a substring of the list1 item (__contains__) being iterated over.

Example:

In [92]: list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
    ...: list2 = [100, 200]
    ...: 

In [93]: [i for i in list1 if any(i for j in list2 if str(j) in i)]
Out[93]: ['bj-100-cy', 'sh-200-pd']
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I try with your script for my real data (which quasi same as example data here), I need to add list2 = [str(x) for x in list2] otherwise I got TypeError: 'in <string>' requires string as left operand, not int
@ahbon Thats strange, because i did type conversion in str(j): any(i for j in list2 if str(j) in i)
Yeah, I reread the data and tried again, it's ok right now, thanks for all helps for this poster.
3

You can use any:

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]
list2 = [str(x) for x in list2]

outcome = [s for s in list1 if any(x in s for x in list2)]

any returns True if any of the conditions you give it are True.

Comments

2
list1 = str(list1)
list2 = str(list2)

You are converting your list into a string with the above statements. So when you iterate in a for loop, you are iterating each characters, instead of each word.

So you should remove string conversion and instead do a list comprehension as follows. Also, in your outcome file instead of checking if the word in list2 is in list1, you are checking the opposite. So you got like 100 and 200 as chars which are in list 2.

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]
outcome = [x for x in list1 for y in list2 if str(y) in x]

Comments

1

You can try this one:

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]

outcome = []
for item in list1:
    if any(str(i) in item for i in list2):
        outcome.append(item)

output:

['bj-100-cy', 'sh-200-pd']

Comments

1

Another alternative list comprehension :

>>> list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
>>> list2 = [100, 200]
>>> occur = [i for i in list1  for j in list2 if str(j) in i]
>>> occur
['bj-100-cy', 'sh-200-pd']

Comments

1

You can use builtin filter method to filter the list based on your condition. Your condition requires python in operator to search for needle([100, 200]) in haystack ([['bj-100-cy','bj-101-hd',...]]). We can use contains method to simplify search syntax.

Code

from operator import contains
filter(lambda x: any(contains(x,str(y)) for y in list2), list1)

Example

>>> list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
>>> list2 = [100, 200]
>>> for item in filter(lambda x: any(contains(x,str(y)) for y in list2), list1):
...     print(item)
...
bj-100-cy
sh-200-pd

Comments

1

You can use regex:

import re

list1 = ['bj-100-cy', 'bj-101-hd', 'sh-200-pd', 'sh-201-hp']
list2 = [100, 200]

pattern = re.compile('|'.join(map(str, list2)))
list(filter(pattern.search, list1))
# ['bj-100-cy', 'sh-200-pd']

Comments

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.