Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Comprehension for Strings

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])  
like image 251
achaud Avatar asked Apr 07 '18 01:04

achaud


People also ask

Can you use list comprehension on a string?

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.

What is list comprehension method?

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.

What is list comprehension explain with an example?

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.

How do I turn a list into a string?

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.


2 Answers

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

EDIT

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': []}
like image 98
MushroomMauLa Avatar answered Oct 04 '22 09:10

MushroomMauLa


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.

like image 44
codehearts Avatar answered Oct 04 '22 09:10

codehearts