【发布时间】:2019-12-31 01:59:36
【问题描述】:
例如,我有 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/")]
【问题讨论】:
-
[i for i in a if i not in b] -
c=[el for el in a if el not in b]。不值得回答 -
我的意思是......我只是在盯着这个,我觉得这很有效。有什么问题?
-
@JoshuaSchlichting 我不知道为什么这对我不起作用
-
@kshnkvn 我刚刚运行了这个,它可以工作。你遇到了什么错误?
标签: python-3.x