這是一個可能愚蠢的問題,但看着the mapping of operators to functions我注意到,沒有功能來表示not in
運營商。起初我以爲這可能是因爲解釋器重新排序爲not x in y
,但有一個is not
的功能,它看起來應該與not in
完全一樣。我錯過了什麼,或者那個操作員真的不存在?python運營商,沒有運營商的「不在」
這裏是一個非常愚蠢的例子,你可能希望這樣的:
def compare_iter(a,b,func):
return [func(aa,bb) for aa,bb in zip(a,b)]
my_compare=compare_iter(xx,yy,lambda x,y:x not in y) #lambda -- yuck
my_compare=map(operator.not_,compare_iter(xx,yy,operator.contains) #extra map? grr...
#it would be nice to do: my_compare=compare_iter(xx,yy,operator.not_contains)
當然,我可以寫我自己的這個功能,但是你的效率,而運營商模塊付出的代價可能會出推動這個代碼的python,因此執行速度更快。
確實,不能'a不是b'只是被重新排序爲'不是a是b'? – JAB 2012-07-11 14:51:51
有沒有檢查'不在'可能比檢查'in'更快的情況? – 2012-07-11 14:53:29
@PaulManta令人懷疑,因爲'不在'根據定義是一個徹底的搜索。 – 2012-07-11 14:56:19