码迷,mamicode.com
首页 > 其他好文 > 详细

使用字典

时间:2017-10-18 21:40:00      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:使用   file   uid   swa   item   print   hotmail   oop   items   

# Filename: using_dict.py

# ‘ab‘ is short for ‘a‘ddress‘b‘ook

ab={ ‘Swaroop‘ : ‘swaroopch@byteofpython.info‘,
‘Larry‘ : ‘larry@wall.org‘,
‘Matsumoto‘ : ‘matz@ruby-lang.org‘,
‘Spammer‘ : ‘spammer@hotmail.com‘
}

print "Swaroop‘s address is %s" %ab[‘Swaroop‘]

# Adding a key/value pair
ab[‘Guido‘]=‘guido@python.org‘

# Deleting a key/value pair
del ab[‘Spammer‘]

print ‘\nThere are %d contacts in the address-book\n‘ %len(ab)
for name,address in ab.items():
print ‘Contact %s at %s‘ %(name,address)

if ‘Guido‘ in ab: # OR ab.has_key(‘Guido‘)
print "\nGuido‘s address is %s" %ab[‘Guido‘]

使用字典

标签:使用   file   uid   swa   item   print   hotmail   oop   items   

原文地址:http://www.cnblogs.com/nku-wangfeng/p/7688929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!