例如,我有2个列表:
a = ['podcast', 'podcasts', 'history', 'gossip', 'finance', 'business', 'kids', 'motivation', 'news', 'investing']
b = ['podcast', 'history', 'gossip', 'finance', 'kids', 'motivation', 'investing']
我想在列表a
中找到不在列表b
中的项目
我尝试这样做:
c = []
for _ in a:
if _ not in b:
c.append(_)
最初,我有一个带有关键字的文本文件:
podcast
podcasts
history
gossip
finance
对于几乎所有关键字,我都有带有信息的文本文件:
podcast.txt
podcasts.txt
history.txt
我需要找到我丢失的文件 我加载了这样的关键字列表:
a = []
with open("keywords.txt", "r") as f:
text = f.read().split("\n")
for k in text:
a.append(k)
b = [e.replace(".txt", "") for e in os.listdir("profiles/")]
答案 0 :(得分:0)
您可以尝试使用numpy吗?
import numpy as np
list1 = [.....]
list2 = [.....]
diff = np.setdiff1d(list2,list1)
答案 1 :(得分:0)
尝试:
c = (set(a) - set(b))
答案 2 :(得分:0)
您可以使用列表理解:
c = [i for i in a if i not in b]
print(c)
['podcasts', 'business', 'news']