簡體   English   中英

另一個列表中一個列表中的項目

[英]items from a list in another list

我有兩個清單:

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

我想知道如何打印list2中包含list1中任何項目(至少1個)的任何項目? 例如,在我們的示例中,應打印以下內容:

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

我寫了一段代碼,但是我的應用程序在運行時壞了:

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

試試看:

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)

輸出:

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

嘗試這個! 它來自內存,但它應該可以工作:)

# 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)

讓我知道是否有誤,

需要拆分成單詞並進行匹配。

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)

輸出

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

這是一個易於閱讀的多行解決方案,該解決方案未考慮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)

這也是一個單行樣式的解決方案(我已經看到@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))]

嘗試這個 :-)

list3的結果是

[“昨天我回家”,“我喜歡踢足球”,“我乘公共汽車去學校”,“我家附近有一個公共汽車站”]

如果直接檢查string中的'bus'會發生什么,您會得到錯誤的結果,因為它將檢查sub_string by_ sub_string,因此如果在此示例中執行此操作:

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

所以解決方案不是檢查sub_string,而是逐字檢查:

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

輸出:

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