设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 10811|回复: 1
打印 上一主题 下一主题

求助!字典的添加,插到哪个位置?

[复制链接]

5

主题

0

好友

69

积分

注册会员

Rank: 2

跳转到指定楼层
楼主
发表于 2013-11-18 14:34:12 |只看该作者 |倒序浏览
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'排在第二位






回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2013-11-20 19:56:21 |只看该作者
和在另一个帖里说的问题一样,字典本身是无序的。
可以通过sorted来排序
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-6-29 11:29 , Processed in 0.022749 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部