I have two lists as shown below. I want to find whether the strings inside the first list are in any of the strings in the second list. For some reason, I get an empty list when I try to run this.
For example: in list5
, string 'apple'
is in list6
's 'I ate an apple'
.
list5 = ['apple', 'mango', 'sherbet']
list6 = ['I ate an apple', 'I ate two apples', 'I love mango']
print ([x for i,x in enumerate(list5) if x in list6])
The exact same things run smoothly for a list of integers. Is there a different way of going through the strings?
list7 = [1, 2, 3, 4, 5]
list8 = [1, 2]
print ([x for i,x in enumerate(list7) if x in list8])
It can identify when it receives a string or a tuple and work on it like a list. You can do that using loops. However, not every loop can be rewritten as list comprehension. But as you learn and get comfortable with list comprehensions, you will find yourself replacing more and more loops with this elegant syntax.
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.
A Python list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element in the Python list. Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list.
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
You are checking if apple
is in the list but you want to check if any string in the list contains apple
list5 = ['apple', 'mango', 'sherbet']
list6 = ['I ate an apple', 'I ate two apples', 'I love mango']
[x for x in list5 if any(x in item for item in list6)]
#['apple', 'mango']
This creates a list of list in which contain the indexes of the sentence in the second list, which contain the word from the first list
list5 = ['apple', 'mango', 'sherbet']
list6 = ['I ate an apple', 'I ate two apples', 'I love mango', '2 apple', 'big apple', 'big mango']
[[i for i, sentence in enumerate(list6) if x in sentence] for x in list5]
#[[0, 1, 3, 4], [2, 5], []]
As I said in my comment a dictionary would be better in this situation
{x:[i for i,sen in enumerate(list6) if x in sen] for x in list5}
#{'apple': [0, 1, 3, 4], 'mango': [2, 5], 'sherbet': []}
And if only exact matches should be stored you can use this, but this does not work if the first list does not only contain words, for example if list5
contains "an apple" this does NOT work
{x:[i for i,sen in enumerate(list6) if any(x==item for item in sen.split())] for x in list5}
#{'apple': [0, 3, 4], 'mango': [2, 5], 'sherbet': []}
Your code is checking if x
is a member of list6
, when it seems like you want to know if x
is a substring of any member in list6
. You can do this using Python's reduce
method (in functools
with Python 3).
list5 = ['apple', 'mango', 'sherbet']
list6 = ['I ate an apple', 'I ate two apples', 'I love mango']
from functools import reduce
print([x for x in list5
if reduce(lambda exist, s: exist or (x in s), list6, False)])
# ['apple', 'mango']
The reduce
call is iterating through all members of list6
and checking if x
is a substring, then or'ing those results against exist
(which defaults to False
here) to return True
if it's found at least once. See the Python3 documentation for functools.reduce for a better idea of how it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With