Substring filter list elements by another list in Python

General Tech Bugs & Fixes 2 years ago

0 5 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (5)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

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?

profilepic.png
manpreet 2 years ago

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']

0 views   0 shares

profilepic.png
manpreet 2 years ago

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.


0 views   0 shares

profilepic.png
manpreet 2 years ago
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]

0 views   0 shares

profilepic.png
manpreet 2 years ago

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']

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.