8
對於內置Python容器(list
,tuple
,等等)in
運算符相當於any(y == item for item in container)
需要提醒的是前一種方法更快(漂亮):Python列表是否具有與__contains__等效的測試身份的功能?
In [13]: container = range(10000)
In [14]: %timeit (-1 in container)
1000 loops, best of 3: 241 us per loop
In [15]: %timeit any(-1 == item for item in container)
1000 loops, best of 3: 1.2 ms per loop
是否有一個相當於any(y is item for item in container)
?也就是說,使用is
而不是==
的測試?
事實上,我一直認爲這是一個文檔的缺陷,方法'list.index'和'list.count'等說幾句,大意「如果沒有這樣的項目「 – mgilson 2012-08-15 14:40:28
」,則返回第一個項目列表中的索引,但是即使對於集合,「in」也檢查不保證*的散列值對象是不同的。考慮'a =(1,2,3); C =(1,2,3); c是a; s = set([a]); c in s'。儘管目前我想不出一個重要的時間。 – mgilson 2012-08-15 14:47:55
沒有,只是指出在set和dicts中的'in'運算符沒有* do *掃描。 – 2012-08-15 14:49:24