迭代字典按顺序返回键 [英] Iterating over dictionary returns keys in sorted order

查看:120
本文介绍了迭代字典按顺序返回键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于python如何处理数据在字典中的问题。让我说我有一个简单的字典,一个数字作为键,一个数字作为如下所示的值:

  a = 5:3,20:1,1:1,5:2,100:3,11:6,
14:1,15:2,16:4,17:2,25:1,19: 1}

我想迭代这个字典并打印出关键字。每次我循环通过字典(如下图所示),它会以增加的顺序打印键。



这是我想要的,但我想知道,我自己的知识,为什么会发生这种情况?它每次都以递增的顺序自动排序吗?正如你可以在上面的字典中看到的,钥匙显然不是增加的顺序,但下面的输出打印他们的增加的顺序。



我只是想获得一个明确的理解,任何帮助将不胜感激。感谢



示例



  for a in a:
打印i

输出

  1 
5
11
14
15
16
17
19
20
25
100


解决方案

词典中的整数并不总是按键排列:

  a = {2:0,9:0 } 
打印a.keys()#[9,2]

Python字典是 哈希表 ,这是一种特殊类型的数组,其中值的单元格的索引导出为应用特殊函数(让我们称之为哈希函数)
这样,如果要为特定的检索,则可以再次计算哈希 键的功能的功能,将返回与之前相同的结果,获取存储的索引。



哈希函数转换 多数 数据类型为整数:

 打印哈希(1)#1 
打印哈希('hello')#840651671246116861
打印哈希((2,3))#3713082714463740756

每种类型都可以定义自己的方式来计算哈希 int 通常返回自身:

 打印哈希(1)#1 
打印散列(20)#20
打印哈希( 1000)#1000

正如你可以看到数字很快变大,我们不想有数组与 840651671246116861 单元格保存字符串 hello
为了避免这个问题,我们可以使用 n 元素创建一个数组,然后使用哈希的剩余部分除以 n 作为索引



例如,如果我们要找到 hello 8 元素数组中:

 打印哈希('hello')%8#5 

所以我们的字典会知道 / em>为密钥 hello 在索引 8 。这就是字典的实现方式。



所以,为什么 {2:0,9:0} 钥匙?这是因为python字典是使用 8 元素创建的,并根据需要增长(更多的是在这个 here )。



我们来计算索引以存储 key = 2 key = 9 n = 8 的字典中:

 打印哈希(2)%8#2 [哈希(2)= 2和2%8 = 2] 
打印哈希(9)%8 #1 [hash(9)= 9 and 9%8 = 1]

这意味着<包含字典数据的数组将是:

  index |关键|值| 
| ------- | ----- | ------- |
| 0 | | |
| 1 | 9 | 0 |
| 2 | 2 | 0 |
| 3 | | |
| 4 | | |
| 5 | | |
| 6 | | |
| 7 | | |

当迭代它时,订单将是此表示中提供的订单,因此 9 将在 2 之前。



您可以阅读更多关于该主题的内容 here


I have a question about how python handle data in dictionaries. Lets say I have a simple dictionary with a number as the key and a number as the value as shown below:

a = { 5: 3, 20: 1, 1: 1, 5: 2, 100: 3, 11: 6,
     14: 1, 15: 2, 16: 4, 17: 2, 25: 1, 19: 1 }

I want to iterate through this dictionary and print out the keys. Every time I loop through the dictionary (as shown below) it prints the keys in increasing order.

This is what I want it to do, but I was wondering, for my own knowledge, why does this happen? Does it auto sort it in increasing order every time? As you can see in the dictionary above, the keys are clearly not in increasing order but the output below prints them in increasing order.

I'm just trying to gain a clear understanding, any help would be greatly appreciated. Thanks

Example

for i in a:
    print i

Output:

1
5
11
14
15
16
17
19
20
25
100

解决方案

Integers in a dictionary are not always ordered by the key:

a = {2:0, 9:0}
print a.keys()  # [9, 2]

Python dictionaries are Hash Tables, which are a special kind of array, where the index of the cell where you store the value is derived applying a special function (let's call it the hash function) on the key. This way if you want to retrieve the value for a particular key you can compute again the hash function of the key, which will return the same result as before, obtaining the index where the value is stored.

The hash function converts most types of data to an integer:

print hash(1)             # 1
print hash('hello')       # 840651671246116861
print hash((2,3))         # 3713082714463740756

Each type can define its own way to compute the hash and int usually returns itself:

print hash(1)             # 1
print hash(20)            # 20
print hash(1000)          # 1000

As you can see numbers get big soon, and we don't want to have an array with 840651671246116861 cells just to save the string hello. To avoid the problem we can create an array with n elements and then use the remainder of the hash divided by n as the index.

For example if we want to find the index for hello in an array of 8 elements:

print hash('hello') % 8   # 5

So our dictionary will know that the value for the key hello is at index 8. That's how dictionaries are implemented.

So, why {2:0, 9:0} is not ordered on keys? That's because python dictionaries are created with 8 elements, and grow as needed (more on this here).

Let's compute the index to store the data having key = 2 and key = 9 in a dictionary with n = 8:

print hash(2) % 8         # 2  [hash(2) = 2 and 2 % 8 = 2]
print hash(9) % 8         # 1  [hash(9) = 9 and 9 % 8 = 1]

This means that the array that contains the dictionary data will be:

| index | key | value |
|-------|-----|-------|
|   0   |     |       |
|   1   |  9  |   0   |
|   2   |  2  |   0   |
|   3   |     |       |
|   4   |     |       |
|   5   |     |       |
|   6   |     |       |
|   7   |     |       |

When iterating over it, the order will be the one presented in this representation, so 9 will be before 2.

You can read more on the topic here.

这篇关于迭代字典按顺序返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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