当前位置: 亚洲城ca88 > ca88 > 正文

Dictionary setdefault

时间:2019-06-13 23:05来源:ca88
#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')pr
#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

编辑:ca88 本文来源:Dictionary setdefault

关键词: 亚洲城ca88