基于键值映射,
user1 = {};
user1['name'] = 'woxiaoe'
user1['city'] = 'cs'
user1['age'] = 1
print user1
user2 = {};
user2['name'] = 'yiba'
user2['city'] = 'cs'
user2['age'] = 1
mail = {'from':user1,'to':user2,'content':'hello world','file':['api.chm']}
print mail
print 'mail[content]\t' + mail['content']
print 'from user name\t' + mail['from']['name']
mail['file'].append('python.doc');
print mail
#dictionary sort
a = {'a':1,'b':2,'c':3}
print 'before sort:'
print a
print "a.keys(): "
print a.keys()
k = a.keys();
k.sort();
print "a.keys().sort():"
print k
print 'sorted(a):'
print sorted(a)
for key in sorted(a):
print a[key]
output
{'city': 'cs', 'age': 1, 'name': 'woxiaoe'}
{'content': 'hello world', 'to': {'city': 'cs', 'age': 1, 'name': 'yiba'}, 'from': {'city': 'cs', 'age': 1, 'name': 'woxiaoe'}, 'file': ['api.chm']}
mail[content] hello world
from user name woxiaoe
{'content': 'hello world', 'to': {'city': 'cs', 'age': 1, 'name': 'yiba'}, 'from': {'city': 'cs', 'age': 1, 'name': 'woxiaoe'}, 'file': ['api.chm', 'python.doc']}
before sort:
{'a': 1, 'c': 3, 'b': 2}
a.keys():
['a', 'c', 'b']
a.keys().sort():
['a', 'b', 'c']
sorted(a):
['a', 'b', 'c']
1
2
3
4484

被折叠的 条评论
为什么被折叠?



