Python中字典的has_key方法在3.4版本中改为in

1 >>> a={1:a,2:b}
2 >>> 1 in a
3 True
4 >>> a in a
5 False
6 >>> 2 in a
7 True
8 >>> 

即可以判断某个键是否存在于字典中;

1 >>> a.has_key(1)
2 Traceback (most recent call last):
3   File "<pyshell#27>", line 1, in <module>
4     a.has_key(1)
5 AttributeError: ‘dict‘ object has no attribute ‘has_key‘

 

文章来自:http://www.cnblogs.com/yuzhuwei/p/4157278.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3