imi*_*org 1 python list-comprehension python-2.6
我在Python 2.6,a和b中有两个非常大的列表(比如50,000个字符串).
这有两个选择.哪个更快,为什么?有没有更好的办法?
c = [i for i in a if i not in b]
Run Code Online (Sandbox Code Playgroud)
要么...
c = list(a) # I need to preserve a for future use, so this makes a copy
for x in b:
c.remove(x)
Run Code Online (Sandbox Code Playgroud)
使用集:
c = list(set(a).difference(b))
Run Code Online (Sandbox Code Playgroud)
或者,如果订单很重要:
set_b = set(b)
c = [i for i in a if i not in set_b]
Run Code Online (Sandbox Code Playgroud)