找到两个列表的差异时如何维护输出列表的顺序

Pra*_*mar 3 python arraylist

我必须找到一个列表与另一个列表之间的区别。

但是这种发现差异的过程使我每次跑步时的输出完全随机化。

下面是我的脚本

getALL = ["apple","ball","cat","dog","eagle"]  // initial list


Sourcee = ["eagle", "ball"]
diff = list(set(getALL) - set(Sourcee))
for items in diff:
    print(items)
Run Code Online (Sandbox Code Playgroud)

有什么办法可以使diff列表的顺序与之相同getALL

我想要这样的输出:

apple
cat
dog
Run Code Online (Sandbox Code Playgroud)

Sun*_*tha 5

只需列表理解即可。(可选)转换Sourceeset可以使其速度更快

>>> source_set = set(Sourcee)
>>> [e for e in getALL if e not in source_set]
['apple', 'cat', 'dog']
Run Code Online (Sandbox Code Playgroud)