为什么不按创建顺序存储Python词典? [英] Why are Python dictionaries NOT stored in the order they were created?

查看:98
本文介绍了为什么不按创建顺序存储Python词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好奇比什么都重要,但是为什么不像下面的字典那样不按创建的顺序排序呢?但是当我打印出test时,它会从此返回相同的顺序...

Just curious more than anything else, but why isn't a dictionary such as the one below not ordered the same as it was created? But when I print out test it returns the same order from then on...

test = {'one':'1', 'two':'2', 'three':'3', 'four':'4'}

这不是我需要订购它们,但是关于这里发生的事情我只是想了一段时间.

It's not that I need them ordered, but it's just been on my mind for awhile as to what is occurring here.

我唯一发现的是此文章:

Python使用复杂的算法来确定键值对在字典中的存储位置.

Python uses complex algorithms to determine where the key-value pairs are stored in a dictionary.

但是这些复杂算法"是什么,为什么呢?

But what are these "complex algorithms" and why?

推荐答案

Python需要能够快速访问D[thing].

Python needs to be able to access D[thing] quickly.

如果它按照接收值的顺序存储值,那么当您要求输入D[thing]时,它不预先知道它将值放在何处.它必须去查找键thing出现的位置,然后找到该值.由于它无法控制这些密钥的接收顺序,因此平均大约需要N/2步,其中N是其接收的密钥数.

If it stores the values in the order that it receives them, then when you ask it for D[thing], it doesn't know in advance where it put that value. It has to go and find where the key thing appears and then find that value. Since it has no control over the order these are received, this would take about N/2 steps on average where N is the number of keys it's received.

但是,如果相反,它具有可以将thing移入内存中某个位置的功能(称为哈希),则它可以快速获取thing并计算该值,然后检入该内存点.当然,这需要做更多的工作-检查D[thing]是否已实际定义,并检查那些可能已定义D[thing1]D[thing2]的罕见情况,其中thing1thing2恰好是相同的(在这种情况下会发生冲突",而python必须找出新的位置放置其中一个).

But if instead it has a function (called a hash) that can turn thing in to a location in memory, it can quickly take thing and calculate that value, and check in that spot of memory. Of course, it's got to do a bit more overhead - checking that D[thing] has actually been defined, and checking for those rare cases where you may have defined D[thing1] and D[thing2] where the hash function of thing1 and thing2 happen to be the same (in which case a "collision" occurs and python has to figure out a new place to put one of them).

因此,在您的示例中,您可能希望当搜索test['four']时,它只是转到列表中存储的最后一个条目,并说啊哈,那是'4'".但这不能仅仅做到这一点.如何知道four对应于列表的最后一个条目.它可能以任何顺序出现,因此它必须创建其他数据结构,以使其能够快速判断four是最后一个条目.这会花费很多开销.

So for your example, you might expect that when you search for test['four'] it just goes to the last entry in a list it's stored and says "aha, that's '4'." But it can't just do that. How does it know that four corresponds to the last entry of the list. It could have come in any order, so it would have to create some other data structure which allows it to quickly tell that four was the last entry. This would take a lot of overhead.

可以按输入顺序输出内容,但这仍然需要额外的开销来跟踪输入的内容.

It would be possible to make it output things in the order they were entered, but that would still require additional overhead tracking the order things were entered.

这篇关于为什么不按创建顺序存储Python词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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