返回顶部

收藏

Dictionary setdefault

更多
#The setdefault method is somewhat similar to get, except that in addition to the get 
#functionality, setdefault sets the value corresponding to the given key if it is not 
#already in the dictionary:

d = {}
print d.setdefault('name', 'N/A')
print  d

d['name'] = 'Gumby'
d.setdefault('name', 'N/A')

print d

#As you can see, when the key is missing, setdefault returns the default and updates 
#the dictionary accordingly. If the key is present, its value is returned and the 
#dictionary is left unchanged. The default is optional, as with get; if it is left 
#out, None is used:

d = {}
print d.setdefault('name')

print d

标签:Python,Dictionary

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2012-08-24 04:19:00 R中媲美Python Dictionary的神器-hash
  2. 祥子 发表 2014-02-21 07:44:22 Python中的字典
  3. Tamim Shahriar (nore 发表 2012-12-10 17:28:37 Concatenate Two Dictionaries | Python How To?
  4. Tamim Shahriar (nore 发表 2011-10-29 03:28:10 Pythonic way to convert a list into dictionary
  5. 博主 发表 2016-04-12 11:18:20 Python导入模块的几种姿势
  6. cold 发表 2013-04-15 08:09:00 使用Tornado进行网络异步编程
  7. cold 发表 2012-08-10 08:28:00 python里的三目运算
  8. 莫扎特 发表 2016-04-14 10:03:21 面向程序员的数据挖掘指南(四)
  9. kongxx 发表 2016-04-19 01:34:38 Python中ConfigParser模块应用
  10. Fnut 发表 2016-04-26 04:45:35 以欧洲组织为目标的基于python的恶意软件家族PWOBot
  11. admin 发表 2016-04-25 09:36:47 PostgreSQL连接Python
  12. kongxx 发表 2016-05-04 10:01:12 Django使用Profile扩展User模块

发表评论