我得找出一张单子和另一张单子的区别。你知道吗
但是这种寻找差异的过程在我每次运行时都会随机输出。你知道吗
下面是我的剧本
你知道吗
getALL = ["apple","ball","cat","dog","eagle"] // initial list
Sourcee = ["eagle", "ball"]
diff = list(set(getALL) - set(Sourcee))
for items in diff:
print(items)
有没有办法保持diff
列表的顺序和getALL
相同?你知道吗
我希望我的输出是这样的:
apple
cat
dog
只需要一个列表就行了。有选择地将
Sourcee
转换成set
会使它更快set操作不保留顺序。但是,您可以通过检查原始列表中的顺序来重新构建diff列表。这适用于任意顺序。如果原始列表包含重复项,则会使问题复杂化。你知道吗
使用
sorted
:即使如此:
以及:
是最短的解决方案。你知道吗
它们都输出:
相关问题 更多 >
编程相关推荐