返回顶部

收藏

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. Tamim Shahriar (nore 发表 2011-10-29 03:28:10 Pythonic way to convert a list into dictionary
  2. 博主 发表 2012-08-24 04:19:00 R中媲美Python Dictionary的神器-hash
  3. 祥子 发表 2014-02-21 07:44:22 Python中的字典
  4. Tamim Shahriar (nore 发表 2012-12-10 17:28:37 Concatenate Two Dictionaries | Python How To?
  5. 潇湘 发表 2014-11-18 08:18:46 Pelican 的入门体验
  6. 博主 发表 2012-07-16 16:00:00 python自动下载chromium最新版
  7. 博主 发表 2013-08-30 00:00:00 python decorator part1
  8. naich 发表 2014-06-02 08:21:16 成交持仓量表的抓取
  9. donaldhuan 发表 2013-01-09 13:33:57 新浪微博传播路径图制作
  10. Tark Rul 发表 2014-03-02 07:01:27 小工具:SimpleRenameTool v0.7
  11. 博主 发表 2014-04-28 07:54:42 【转】python 调试工具 pdb的使用
  12. hushlight 发表 2015-04-19 03:46:54 Leetcode – Maximum Gap