Moe
Moe

Reputation: 67

items from a list in another list

I have two lists:

list1 = ['home', 'school', 'bus', football']
list2 = ['yesterday I went to home', 'I am busy', 385723, 'I feel like 
         playing football', I was tired last week', 'I go to school by 
         bus',' there is a bus stop near my home', 'I am hungry']

I wanted to know how can I print any item from list2 that contains any item(at least 1) from list1? for example in our example the following should be printed:

'yesterday I went to home','I feel like playing football', 'I go to school 
 by bus',' there is a bus stop near my home'

I wrote a piece of code but my application breaks when I run it:

    theList = []
    i = 0
    while i < len(list1):
        for element in list2:
            if (list1[i]) in element.lower():
                theList.append(element)
        i += 1
    print(errorList)

Upvotes: 1

Views: 113

Answers (8)

Pabasara Ranathunga
Pabasara Ranathunga

Reputation: 170

list3 = [s for s in list2 if (type(s)==str) and (set(s.split())&set(list1))]

Try this :-)

the result for list3 is

['yesterday I went to home', 'I feel like playing football', 'I go to school by bus', ' there is a bus stop near my home']

Upvotes: 0

Aaditya Ura
Aaditya Ura

Reputation: 12689

There is a thing if you directly check if 'bus' in string , you can get wrong result because it will check sub_string by_ sub_string, so if you do this on this example:

'bus' in 'I am busy' then it will return True :

So solution is instead of checking sub_string , check word by word :

print([j for i in list1 for j in list2 if isinstance(j,str) if i in j.split()])

output:

['yesterday I went to home', ' there is a bus stop near my home', 'I go to school by bus', 'I go to school by bus', ' there is a bus stop near my home', 'I feel like playing football']

Upvotes: 0

Laurent H.
Laurent H.

Reputation: 6536

Here is an easily readable multi-line solution, which does not take into account the items of list2 that are not of string type:

result = []
for sentence in list2:
    if isinstance(sentence, str):
        for word in list1:
            if word in sentence.split():
                result.append(sentence)
                break
print(result)

Here is also a one-line style solution (I saw it has been already given by @grimek), which converts any items from list2 into stringtype:

result = [sentence for sentence in list2 if any(word in str(sentence).split() for word in list1)]
print(result)

Upvotes: 0

SkyLine
SkyLine

Reputation: 54

list1 = ['home', 'school', 'bus', 'football']
list2 = ['yesterday I went to home', 'I am busy', '385723', 
        'I feel like playing football', 'I was tired last week', 
        'I go to school by bus',' there is a bus stop near my home', 
        'I am hungry']

ans = []
for s2 in list2:
    l2 = s2.split(' ')
    for ss in l2:
        if ss in list1:
           ans.append(s2)
           break
print ans

Upvotes: 0

Junhee Shin
Junhee Shin

Reputation: 758

need to split into words and match.

list1 = ['home', 'school', 'bus', 'football']
list2 = ['yesterday I went to home', 'I am busy', 385723, 'I feel like playing football',
         'I was tired last week', 'I go to school by bus',' there is a bus stop near my home', 'I am hungry']

theList = []
for item2 in list2:
    for item1 in list1:
        bfound=False
        if type(item2) is  str:
            words = item2.lower().split()
            for word in words:
                if word==item1:
                    theList.append(item2)
                    bfound=True
                    break
            if bfound:
                break

print(theList)

output

['yesterday I went to home', 'I feel like playing football', 'I go to school by bus', ' there is a bus stop near my home']

Upvotes: 0

Steven Black
Steven Black

Reputation: 2232

try this! it's from memory but it should work :)

# Print Your code here
print('Hello in console window')

list1 = ['home', 'school', 'bus', 'football']
list2 = ['yesterday I went to home', 'I am busy', 385723,
         'I feel like playing football', 'I was tired last week'
         , 'I go to school by bus'
         ,' there is a bus stop near my home', 'I am hungry']

theList = []
i = 0
for item in list2:
    item1 = str(item).split(" ")
    if len(item1) >= 1:
        is_in = [x for x in item1 if x.lower() in list1]
    else:
        is_in = item if item.lower() == list1 else 0
    if len(is_in) > 0:
        theList.append(item)

for item in theList:
    print(item)

Let me know if it errs,

Upvotes: 0

grimek
grimek

Reputation: 106

Another solution:

print([el2 for el2 in list2 if any(el1 in str(el2).split() for el1 in list1)])

Upvotes: 4

Sruthi
Sruthi

Reputation: 3018

Try this out :

for string in list2:
    if(type(string)==str): #if the element in list2 is a string
        if(set(string.split()) & set(list1)): #split the string into words, and check if it has a set intersection with list1
            print(string)

OUTPUT :

yesterday I went to home
I feel like playing football
I go to school by bus
 there is a bus stop near my home

Upvotes: 3

Related Questions