繁体   English   中英

查找Python列表之间不常见的项目

[英]Find items not in common between lists in Python

我在Python 2.6中有两个非常大的列表(例如50,000个字符串), ab

这是两个选择。 哪个更快,为什么? 有没有更好的办法?

c = [i for i in a if i not in b]

要么...

c = list(a)  # I need to preserve a for future use, so this makes a copy
for x in b:
    c.remove(x)

使用集:

c = list(set(a).difference(b))

或者,如果订单很重要:

set_b = set(b)
c = [i for i in a if i not in set_b] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM