use*_*016 0 python optimization for-loop list conditional-statements
我想在一行list中为不存在的项目创建一个循环other_list.像这样的东西:
>>> list = ['a', 'b', 'c', 'd']
>>> other_list = ['a', 'd']
>>> for item in list not in other_list:
... print item
...
b
c
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
for item in (i for i in my_list if i not in other_list):
print item
Run Code Online (Sandbox Code Playgroud)
它有点冗长,但它同样有效,因为它只在下一个循环中呈现每个下一个元素.