简体   繁体   English

另一个列表中一个列表中的项目

[英]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? 我想知道如何打印list2中包含list1中任何项目(至少1个)的任何项目? 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)

另一个解决方案:

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

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

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, 让我知道是否有误,

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

Here is an easily readable multi-line solution, which does not take into account the items of list2 that are not of string type: 这是一个易于阅读的多行解决方案,该解决方案未考虑list2的非string类型的项:

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 string type: 这也是一个单行样式的解决方案(我已经看到@grimek已经给出了),该解决方案将list2所有项目转换为string类型:

result = [sentence for sentence in list2 if any(word in str(sentence).split() for word in list1)]
print(result)
list3 = [s for s in list2 if (type(s)==str) and (set(s.split())&set(list1))]

Try this :-) 尝试这个 :-)

the result for list3 is list3的结果是

['yesterday I went to home', 'I feel like playing football', 'I go to school by bus', ' there is a bus stop near my home'] [“昨天我回家”,“我喜欢踢足球”,“我乘公共汽车去学校”,“我家附近有一个公共汽车站”]

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: 如果直接检查string中的'bus'会发生什么,您会得到错误的结果,因为它将检查sub_string by_ sub_string,因此如果在此示例中执行此操作:

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

So solution is instead of checking sub_string , check word by word : 所以解决方案不是检查sub_string,而是逐字检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM