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']
请问为何一定要用ab.items()?
我用ab.items()会失败
另一个问题是 for in 后面他用name, address
我把他颠倒address, name 后面的% (address, name)也作变动
但是奇怪的是print出来的句子(contact %s at %s部分)
却仍然是contact name at address?
我好像有看到文章说%s 是没有顺序的 就是指这种情况吗?
但是文章又说可以自己指定顺序 想请问一下 以这篇为例要如何去指定顺序呢?
感谢回答~