I wonder what is better to do:
d = {'a': 1, 'b': 2}
'a' in d
True
or:
d = {'a': 1, 'b': 2}
d.has_key('a')
True
本文讨论了在Python中检查字典键存在的两种方法:使用'in'关键字和'dict.has_key()'方法,并强调了Python 3.x中推荐的做法。
|
393
51
|
I wonder what is better to do:
or:
| |||
|
add a comment
|
|
546
|
In fact | |||
|
179
|
While the following observation is not always true, you'll notice that usually, in Python, the faster solution is more elegant and Pythonic; that's why | ||||||||||||
|
|
43
|
According to python docs:
| ||||
|
|
28
|
Use | ||||||||||||
|
|
13
|
There is one example where If you use Fix is obviously trivial:
| ||
|
8
|
|
5720
inworks with 2.6 too right? – Logan Jan 17 '13 at 4:07