[转载] Python 字典(Dictionary) get()方法
参考链接: Python中字典dictionary的get方法
Python 字典(Dictionary) get()方法
描述
Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值。
语法
get()方法语法:
dict.get(key, default=None)
参数
key -- 字典中要查找的键。default -- 如果指定键的值不存在时,返回该默认值值。
返回值
返回指定键的值,如果值不在字典中返回默认值None。
实例
以下实例展示了 get()函数的使用方法:
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 27}
print "Value : %s" % dict.get('Age')
print "Value : %s" % dict.get('Sex', "Never")
以上实例输出结果为:
Value : 27
Value : Never
在Python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False
创建一个字典:
dic={'int':0}
dic.get('name")获取字典的int的值,如果有则返回这个值,如果没有则返回none相当于flase
例如:if dic.get('name'):
print 'name cunzai'
else:
print 'name bucunzai'
但是结果会出现name bucunzai 因为int的值是0,if会默认为flase,所以这种方法存在缺陷,使用另外一种方法:
if dic.get('name') != None:
print 'name cunzai'
else:
print 'name bucunzai;
这样就不会出现上面的情况 --------------------- 作者:qq_41828921 来源:CSDN 原文:https://blog.csdn.net/qq_41828921/article/details/79949754
Python 字典 get() 方法和 setdefault() 方法类似,返回指定键的值,如果键不在字典中,返回一个指定值,默认为None。
get() 和 setdefault() 区别: setdefault() 返回的键如果不在字典中,会添加键(更新字典),而 get() 不会添加键。
[转载] Python 字典(Dictionary) get()方法相关推荐
- Python 字典(Dictionary) get()方法
Python 字典(Dictionary) get()方法 描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: ...
- [转载] 1006- Python 字典(Dictionary) items()方法
参考链接: Python字典dictionary| items()方法 Python 字典的一些操作 目录 用 [TOC]来生成目录: Python 字典的一些操作 目录字典基础 Markdown及扩 ...
- [转载] Python字典的setdefault()方法
参考链接: Python字典setdefault() Python字典的setdefault()方法 (2012-06-22 02:18:05)转载▼ setdefault(key[, default ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典 setdefault() 函数和 get()方法 类似, 如果键不存在于字典中,将会添加键并将值设为默认值. 语法 setdefault() 方法语法: dict.setde ...
- Python 字典(Dictionary. items()方法
以列表返回可遍历的(键, 值) 元组数组 语法:dict.items() 返回值:返回可遍历的(键, 值) 元组数组 dict = {'Google': 'www.google.com', 'Runo ...
- Python 字典(Dictionary) copy()方法
https://www.runoob.com/python/att-dictionary-copy.html
- [转载] python cmp函数比较字典_Python 字典(Dictionary) cmp()方法
参考链接: Python cmp方法 Python 字典(Dictionary) cmp()方法 描述 Python 字典(Dictionary) cmp() 函数比较两个字典元素. 语法 cmp() ...
- [转载] python 字典的get()pop()方法的区别
参考链接: Python字典dictionary| pop方法 get()方法 返回指定键 key 的值,如果值不在字典中返回默认值.语法:dict.get(key, default=None) 参数 ...
- python中len字典_Python 字典(Dictionary) len()方法
Python 字典(Dictionary) len()方法 描述 Python 字典(Dictionary) len() 函数计算字典元素个数,即键的总数.高佣联盟 www.cgewang.com 语 ...
最新文章
- CentOS 7磁盘格式化
- UVA1396 Most Distant Point from the Sea(AM - ICPC - Tokyo - 2007)(计算几何,半平面交 + 二分答案)
- SAP RETAIL Rapid Replenishment
- 用户路径分析:揭秘你的用户行为偏好习惯
- Delphi 中自定义异常及异常处理的一般方法
- f-GAN简介:GAN模型的生产车间
- Windows Server 2008 R2 负载平衡入门篇
- eclipse 下使用git clone
- hadoop集群中客户端修改、删除文件失败
- layui获得列表json数据_golang实战开发之博客功能篇:文章列表的读取与展示和分类筛选展示处理...
- Java开发的可扩展-高性能-响应式的API网关-soul v2.2.1
- Namomo Spring Camp Div2 Week1 - 第五次打卡
- 让Kubernetes成为数据中心操作系统的一等公民
- 【设计模式】第六章 观察者模式
- 远程桌面管理工具源码
- 种草电商系统种草电商系统开发解决方案
- 请各位大虾帮忙!小女子谢过了!:)
- 【实战篇】37 # 如何使用 QCharts 图表库绘制常用数据图表?
- Mxnet训练自己的数据集并测试
- Java---身份证号查询, 手机号查询
热门文章
- UVa815 - Flooded!
- pscad与matlab接口,PSCAD/EMTDC与Matlab接口研究
- 全栈性能测试修炼宝典jmeter实战电子版_推荐一款技术人必备的接口测试神器:Apifox...
- python网络编程基础知识_python网络编程基础
- 读者问题:3种解决办法伪代码
- Brupsuit的安装与初步使用
- Codecraft-18 and Codeforces Round #458: C. Travelling Salesman and Sp(组合数)
- bzoj 4131: 并行博弈(博弈)
- bzoj 3884: 上帝与集合的正确用法(欧拉函数)
- bzoj 4236: JOIOJI(map+pair)