小编fou*_*ing的帖子

Python dict.get('key')与dict ['key']

为什么会抛出一个KeyError:

d = dict()
d['xyz']
Run Code Online (Sandbox Code Playgroud)

但这不是吗?

d = dict()
d.get('xyz')
Run Code Online (Sandbox Code Playgroud)

如果描述符在这里起作用,我也很好奇.

python dictionary descriptor keyerror

11
推荐指数
2
解决办法
8811
查看次数

Python字典中的整数键是否按优先级排序和排序?

我在OS X Mavericks上使用python 2.7.5,并且看到字典用于生成简单文本菜单的行为异常。我的问题是:Python字典中的整数键是否按优先级排序和排序? 我可以看到mainMenu_1字典(包含一些数字键和一些字符串键)对整数键进行排序,然后以预期的随机顺序显示字符串键。mainMenu_2按预期随机化。

来自python 2.7.8 docs:

“最好将字典视为一组无序的键:值对,要求键是唯一的(在一个字典中)。”

mainMenu_1 = {
   0: 'README',
   1: 'New Set',
   2: 'View Sets',
   3: 'Quiz',
   4: 'Scores',
   5: 'Configuration Settings',
   'Q': 'Quit',
   'a': 'additional letter to test',
   'b': 'additional letter to test'
}

mainMenu_2 = {
   'one': 'README',
   'two': 'New Set',
   'three': 'View Sets',
   'four': 'Quiz',
   'five': 'Scores',
   'six': 'Configuration Settings',
   'Q': 'Quit',
   'd': 'another letter to test'
}

print mainMenu_1.keys()
[0, 1, 2, 3, 4, 5, 'a', …
Run Code Online (Sandbox Code Playgroud)

python dictionary

1
推荐指数
1
解决办法
841
查看次数

标签 统计

dictionary ×2

python ×2

descriptor ×1

keyerror ×1