Crossin的编程教室

标题: 求助!字典的添加,插到哪个位置? [打印本页]

作者: hunsjie    时间: 2013-11-18 14:34
标题: 求助!字典的添加,插到哪个位置?
https://learn-python-the-hard-way-zh_cn-translation.readthedocs.org/en/latest/ex39.html

>>> stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}
>>> print stuff['name']
Zed
>>> print stuff['age']
36
>>> print stuff['height']
74
>>> stuff['city'] = "San Francisco"
>>> print stuff['city']
San Francisco
>>>

>>> stuff[1] = "Wow"
>>> stuff[2] = "Neato"
>>> print stuff[1]
Wow
>>> print stuff[2]
Neato
>>> print stuff
{'city': 'San Francisco', 2: 'Neato',
    'name': 'Zed', 1: 'Wow', 'age': 36,
    'height': 74}
>>>

为什么city,会排在最前,1:'Wow'排在第4位,2:'Neato'排在第二位







作者: crossin先生    时间: 2013-11-20 19:56
和在另一个帖里说的问题一样,字典本身是无序的。
可以通过sorted来排序




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5