例如,我有 2 个列表:
a = ['podcast', 'podcasts', 'history', 'gossip', 'finance', 'business', 'kids', 'motivation', 'news', 'investing']
b = ['podcast', 'history', 'gossip', 'finance', 'kids', 'motivation', 'investing']
Run Code Online (Sandbox Code Playgroud)
我想在列表a中查找不在列表中的项目b
我尝试这样做:
c = []
for _ in a:
if _ not in b:
c.append(_)
Run Code Online (Sandbox Code Playgroud)
最初,我有一个带有关键字的文本文件:
podcast
podcasts
history
gossip
finance
Run Code Online (Sandbox Code Playgroud)
同样对于几乎所有关键字,我都有包含信息的文本文件:
podcast.txt
podcasts.txt
history.txt
Run Code Online (Sandbox Code Playgroud)
我需要找到我丢失的文件我加载了这样的关键字列表:
podcast
podcasts
history
gossip
finance
Run Code Online (Sandbox Code Playgroud)
您可以使用列表理解:
c = [i for i in a if i not in b]
print(c)
['podcasts', 'business', 'news']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4396 次 |
| 最近记录: |