Python字典的数字键是否自动排序? [英] Is numeric keys of Python's dictionary automatically sorted?

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

问题描述

我在python中编写一个基数排序,我认为Python'的字典可能是
可以作为存储桶的选择。


唯一的问题是字典是没有顺序的映射。但我只是

发现如果键是数字的,键本身是在

字典中订购的。


部分我的代码是这样的:

radix = {}

for i in range(256):

radix [i] = []


我查了一下,发现它的订购方式如下:{1:[],2:[],3 [],...}


所以我可以按照所需的顺序打印字典的内容

,无需额外的代码。

我也尝试添加新的数字键,发现字典'的钥匙

仍在订购。


但是,我不确定它是否总是这样。任何人都可以确认我的发现吗?

I am coding a radix sort in python and I think that Python''s dictionary may
be a choice for bucket.

The only problem is that dictionary is a mapping without order. But I just
found that if the keys are numeric, the keys themselves are ordered in the
dictionary.

part of my code is like this:
radix={}
for i in range(256):
radix[i]=[]

I checked and found that it is ordered like: {1:[], 2:[], 3[],...}

So I can just print out the contents of the dictionary in the desired order
without additional code.
I also tried adding new numeric keys and found that the dictionary''s keys
are still ordered.

However, I am not sure whether it is always like this. Can anybody confirm
my finding?

推荐答案

3月7日晚上8:18,约翰" < rds1 ... @ sh163.netwrote:

....
On Mar 7, 8:18 pm, "John" <rds1...@sh163.netwrote:
....

但是,我不确定它是否总是像这个。任何人都可以确认我的发现吗?
However, I am not sure whether it is always like this. Can anybody confirm
my finding?


>来自标准库文档:
>From the standard library docs:



"键和值是以非随机的任意顺序列出,不同的是,
在Python实现中有所不同,并且取决于字典'

的插入和删除历史。


即您发现的行为是一个实现细节,

并且可能在将来的版本中发生变化。

"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary''s
history of insertions and deletions."

i.e. the behaviour you have discovered is an implementation detail,
and could change in future versions.


那么无论如何都要对数字键进行排序并避免将来的实现

混乱?

" Ant" < an **** @ gmail.comwrote in message

news:11 ********************** @ t69g2000cwt.googlegr oups.com ...
Then is there anyway to sort the numeric keys and avoid future implemetation
confusion?
"Ant" <an****@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...

3月7日晚上8:18,John < rds1 ... @ sh163.netwrote:

...
On Mar 7, 8:18 pm, "John" <rds1...@sh163.netwrote:
...

>但是,我不确定它是否总是像这样。任何人都可以确认我的发现吗?
>However, I am not sure whether it is always like this. Can anybody
confirm
my finding?


>>来自标准库文档:
>>From the standard library docs:



" Keys and值以非随机的任意顺序列出,
在Python实现中各不相同,并且取决于字典'

插入和删除的历史记录。


即您发现的行为是一个实现细节,

并且可能在将来的版本中发生变化。


"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary''s
history of insertions and deletions."

i.e. the behaviour you have discovered is an implementation detail,
and could change in future versions.



在2007年3月3日星期三15:18 -0500,John写道:
On Wed, 2007-03-07 at 15:18 -0500, John wrote:

我正在编码一个基数在python中排序,我认为Python的字典可能是
是桶的选择。


唯一的问题是字典是没有顺序的映射。但我只是

发现如果键是数字的,键本身是在

字典中订购的。
I am coding a radix sort in python and I think that Python''s dictionary may
be a choice for bucket.

The only problem is that dictionary is a mapping without order. But I just
found that if the keys are numeric, the keys themselves are ordered in the
dictionary.



No.


字典中的键序列是
$ b $的巧合副作用b特定的Python实现,密钥的数量,密钥的值以及密钥的插入顺序。你不能

依赖于任何特定订单中出现的钥匙。


这是一个简单的反例,打破了订单,至少对于

我正在运行的版本:

No.

The sequence of keys in a dictionary is a coincidental side effect of
the particular Python implementation, the number of keys, the values of
the keys, and the order in which the keys are inserted. You must not
rely on the keys appearing in any particular order.

Here is a simple counterexample that breaks the ordering, at least for
the version I''m running:


>> d = {}
我在范围(0,6)中:d [10 ** i] = []
>>d = {}
for i in range(0,6): d[10**i] = []



....

....


>> d
>>d



{100000:[],1:[],100:[],1000:[],10:[],10000:[]}


-Carsten

{100000: [], 1: [], 100: [], 1000: [], 10: [], 10000: []}

-Carsten


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

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