Python基础—— dict.get() 与 dict['key'] 的区别
来源:互联网 发布:免费刷空间人气软件 编辑:程序博客网 时间:2024/06/06 07:04
In [1]: a = {'name': 'wang'}In [2]: a.get('age')In [3]: a['age']---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-3-a620cb7b172a> in <module>()----> 1 a['age']KeyError: 'age'In [4]: a.get('age', 10)Out[4]: 10
所以,dict[‘key’]只能获取存在的值,如果不存在则触发KeyError
而dict.get(key, default=None)则如果不存在则返回一个默认值,如果设置了则是设置的,否则就是None
In [6]: type(a.get('age'))Out[6]: NoneType
阅读全文
0 0
- Python基础—— dict.get() 与 dict['key'] 的区别
- python dict.get()和dict['key']的使用
- Python基础——dict
- Python 读Dict数据的方法,解决key 不在dict的问题,get()函数,setdefault()函数
- python dict reg key
- Python 基础——dict(二)
- Python 基础 —— dict(三)
- Python——dict
- Python——dict
- python: dict.get()方法
- Python入门——dict与set
- python的dict与set
- python字典(dict)的get方法
- python 字典 dict.get的用法
- Python 3 collections.defaultdict() 与 dict的使用和区别
- Python collections.defaultdict() 与 dict的使用和区别
- Python collections.defaultdict()与dict的使用和区别
- Python collections.defaultdict() 与 dict的使用和区别
- HDU -- 5071 Chat 超强模拟
- Python:矩形覆盖
- 并发队列-无界非阻塞队列 ConcurrentLinkedQueue 原理探究
- numpy基础——ndarray.shape
- StringBuffer的用法
- Python基础—— dict.get() 与 dict['key'] 的区别
- Leetcode Basic Calculator I && II
- 并发队列 – 无界阻塞队列 LinkedBlockingQueue 原理探究
- tensorflow.nn.bidirectional_dynamic_rnn()函数的用法
- Student Attendance Record I
- 排序、树和图
- Java提高篇——单例模式
- PAT---1001. A+B Format(调试过程)
- 我的Mini-Shell