温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How do I individually print items of a list that are in another list in python
list python python-3.6

其他 - 如何单独打印python中另一个列表中的列表项

发布于 2020-03-27 12:08:59

我想单独打印(然后写入文件)另一个列表中的列表项。如果没有匹配的项目,那么我希望打印“无”。我的程序有时间限制,因此我想对此快速且轻松地解决,最好在0.1秒以内。

我有一个名为joinedComb的列表,我想单独打印joindComb中我尝试过的另一个名为字典的列表中的所有项目

for i in joinedCombs:
    if i in dictionary:
        endResult.append(i)
        fout.write(i+'\n')
if endResult == []:
    fout.write('NONE\n')

我希望它打印出这样的内容:

GREG
GEKA
GENO

要么

NONE

查看更多

查看更多

提问者
user11735387
被浏览
123
magma 2019-07-05 06:36
endResult = [i for i in joinedCombs if i in dictionary] 
fout = '\n'.join(endResult) if any(endResult) else 'NONE'

如果您愿意,可以不循环地进行。您可以使用两组逻辑组合,但不要期望缩短执行时间。

endResult = set(joinedCombs).intersection(set(dictionary.keys()))