亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果字符串不在另一個單項字符串列表中,則從列表中刪除字符串

如果字符串不在另一個單項字符串列表中,則從列表中刪除字符串

慕桂英4014372 2023-09-05 21:09:23
我有兩個字符串列表,如下所示:good_tags = ['c#', '.net', 'java']all_tags = [['c# .net datetime'],            ['c# datetime time datediff relative-time-span'],             ['html browser timezone user-agent timezone-offset']]我的目標是僅保留“all_tags”字符串列表中的“good_tags”,例如,'all_tags' 的第一行:[c# .net datetime]應成為(基于我想保留在“good_tags”中的字符串列表):[c# .net]我嘗試使用“in”而不是“not in”,基于從另一個列表中刪除一個列表中出現的所有元素y3 = [x for x in all_tags if x in good_tags]print ('y3: ', y3) y4 = [x for x in good_tags if x in all_tags]print ('y4: ', y4)出去:y3:  []y4:  []
查看完整描述

6 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

good_tags = ['c#', '.net', 'java']


all_tags = [

    ['c# .net datetime'],

    ['c# datetime time datediff relative-time-span'],

    ['html browser timezone user-agent timezone-offset']

]


filtered_tags = [[" ".join(filter(lambda tag: tag in good_tags, row[0].split()))] for row in all_tags]

print(filtered_tags)

輸出:


[['c# .net'], ['c#'], ['']]

>>> 


查看完整回答
反對 回復 2023-09-05
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

第一條語句:當“x in all_tags”執行時,它將給出 ['c# .net datetime'],它是列表類,而 'c# .net datetime' 是單個字符串,不會單獨處理。


第二條語句:在第一條語句 x = ['c# .net datetime'] 之后,即列表,現在該列表將在不包含整個列表的 good_tags 中搜索,因此不會返回任何內容。


條件 1:如果我們的 good_tags 類似于 ['c#', '.net', 'java', ['c# .net datetime'] ] 那么它將返回 ['c# .net datetime']


這是您的解決方案的問題:


good_tags = ['c#', '.net', 'java']


all_tags = [['c# .net datetime'], ['c# datetime time datediff relative-time-span'],

            ['html browser timezone user-agent timezone-offset']]



#y3 = [x for x in all_tags if x in good_tags]

all_tags_refine = []

for x in all_tags:

    y = x[0].split()


    z = [k for k in y if k in good_tags]

    all_tags_refine.append(z)


print(all_tags_refine)


查看完整回答
反對 回復 2023-09-05
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

你的all_tags是一個列表,其中包含三個列表,其中每個列表包含一個字符串。因此,您首先需要做的是將每個子列表轉換為包含字符串的列表,而不僅僅是一個字符串。


由于那里只有空格,用于分隔標簽并且沒有逗號,因此您必須將列表從 轉換['c# .net datetime']為['c#', '.net', 'datetime']:


[x for segments in all_tags[0] for x in segments.split()]

然后您可以對整個列表執行此操作,因此迭代它的長度:


[[x for segments in all_tags[entry] for x in segments.split()] for entry in range(len(all_tags))]

返回:


[['c#', '.net', 'datetime'],

 ['c#', 'datetime', 'time', 'datediff', 'relative-time-span'],

 ['html', 'browser', 'timezone', 'user-agent', 'timezone-offset']]

現在您可以根據您的好標簽過濾此列表:


y3 = [[x for x in [words for segments in all_tags[entry] for words in segments.split()] if x in good_tags] for entry in range(len(all_tags))]

輸出:


[['c#', '.net'], ['c#'], []]


查看完整回答
反對 回復 2023-09-05
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

good_set = set(good_tags)

kept_tags = [[t for t in tags[0].split() if t in good_set] 

    for tags in all_tags]

print(kept_tags)

# [['c#', '.net'], ['c#'], []]


查看完整回答
反對 回復 2023-09-05
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

可能有更好的方法來做到這一點,但就是這樣,


good_tags = ['c#', '.net', 'java']


all_tags = [['c# .net datetime'],['c# datetime time datediff relative-time-span'], ['html browser timezone user-agent timezone-offset']]


for tags in all_tags:

    empty = []

    for tag in tags[0].split(" "):

        if tag in good_tags:

            empty.append(tag)

    print(" ".join(empty))


查看完整回答
反對 回復 2023-09-05
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

首先,您沒有兩個字符串列表。您有字符串列表的列表。


good_tags = ['c#', '.net', 'java']


all_tags = [['c# .net datetime'],['c# datetime time datediff relative-time-span'], ['html browser timezone user-agent timezone-offset']]


all_tags_with_good_tags = []


for tags in all_tags:

    new_good_tags = set()

    for tag in tags[0].split():  # here you have list, so you need to select 0 element 

                                 #  of it as there's only 1 list element in your example 

                                 #  and then split it on the whitespace to be a list of tags

        if tag in good_tags:

            new_good_tags.add(tag)

    if new_good_tags:

        all_tags_with_good_tags.append(' '.join(new_good_tags))


會得到你


['.net c#', 'c#']


查看完整回答
反對 回復 2023-09-05
  • 6 回答
  • 0 關注
  • 234 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號