Python字典中的整数键是否以优先级排序和排序? [英] Are the integer keys in a Python dictionary sorted and sorted with priority?

查看:117
本文介绍了Python字典中的整数键是否以优先级排序和排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



从python 2.7.8 docs:


最好将字典视为一组无序的键:值
对,要求键是唯一的(在一个$ b内



  mainMenu_1 = {
0:'README' ,
1:'New Set',
2:'View Sets',
3:'Quiz',
4:'Scores',
5:'配置设置',
'Q':'退出',
'a':'额外的字母测试',
'b':'额外的字母要测试'
}

mainMenu_2 = {
'one':'README',
'two':'New Set',
'three':'View Sets',
'four':'Quiz',
'five':'Scores',
'six':'Configuration Settings',
'Q':'Quit',
'd':'另一封测试'
}

打印mainMenu_1.keys()
[0,1,2,3,4,5,'a','Q','b']
打印mainMenu_2.keys()
['four','Q','five','three','d','six','two','one']

第三个测试:

  c = {1:'one' 'two':'two',3:'three'} 
print c
{1:'one',3:'three','two':'two'}


解决方案

dict 根据它们的底层哈希表桶,排序(引号大量使用)。一个整数的 hash()本身就是:

  hash )
Out [29]:42

...但这并不一定意味着因为散列采用模数表大小,所以较低的整数将出现在较高的整数之前,以便将其分配给一个存储桶。

  d = {i:i for range in range(250,260)} 

print(d)
{256:256,257:257,258:258,259:259,250:250,251:251,252:252,253:253,254:254,255:255}

所以连续的整数不一定在 dict 中排序。而对于获得优先级的整数,不,整数没有什么特别之处。他们的哈希值很好地聚集在一起,你恰巧选择了一些比这个块更大(再次模数表大小)的字符串。

  hash('roippi')
Out [26]:8915818834981308633

hash('roippi')%8#min py2中的哈希表大小是3位
Out [27]:1

d = {0:'','roippi':0,2:0}

print(d)
{0: '','roippi':0,2:0}

所有这一切当然是一个实现cPython的细节,所以唯一保证的是排序是任意但一致的 1



1 一致,至少,跨越一个单一的Python解释器。 3.2.3+ 随机种子某些(非 - int)类型,早期版本也会这样做,如果使用 -R标志


I'm using python 2.7.5 on OS X Mavericks and I'm seeing unexpected behavior with a dictionary I'm using to generate a simple text menu. My question is: are the integer keys in a Python dictionary sorted and sorted with priority? I can see that the mainMenu_1 dictionary (containing some numeric keys and some string keys) sorts the integer keys and then presents the string keys in the expected random order. mainMenu_2 is randomized as expected.

from the python 2.7.8 docs:

"It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary)."

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', 'Q', 'b']
print mainMenu_2.keys()
['four', 'Q', 'five', 'three', 'd', 'six', 'two', 'one']

And a third test:

c = {1:'one','two':'two',3:'three'}
print c
{1: 'one', 3: 'three', 'two': 'two'}

解决方案

dicts are "sorted" (quotation fingers heavily used) according to their underlying hash table buckets. The hash() of an integer is itself:

hash(42)
Out[29]: 42

...but that doesn't necessarily mean that lower integers will appear before higher ones since the hash is taken modulo table size in order to allocate it to a bucket.

d = {i:i for i in range(250,260)}

print(d)
{256: 256, 257: 257, 258: 258, 259: 259, 250: 250, 251: 251, 252: 252, 253: 253, 254: 254, 255: 255}

So contiguous integers are not necessarily sorted in a dict. And as for integers getting priority, no, there's nothing special about integers. Their hash values just clump together particularly well, and you happened to pick some strings that hashed larger (again, modulo table size) than that clump.

hash('roippi')
Out[26]: 8915818834981308633

hash('roippi') % 8 # min hashtable size in py2 is 3-bit
Out[27]: 1

d = {0:'', 'roippi':0, 2:0}

print(d)
{0: '', 'roippi': 0, 2: 0}

All of this is of course an implementation detail of cPython, so the only thing that is guaranteed is that ordering is "arbitrary but consistent1".

1consistent, at least, across a single run of the python interpreter. 3.2.3+ randomly seeds the hash of certain (non-int) types, and earlier versions will also do so if run with the -R flag.

这篇关于Python字典中的整数键是否以优先级排序和排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆