如何访问字典或散列中的第n项? [英] How do I access the nth item in a dictionary or hash?

查看:181
本文介绍了如何访问字典或散列中的第n项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个键和值的字典,例如:

I have a dictionary of keys and values, for example:

{
    fred: 1,
    dave: 2,
    lily: 3
}

在这种情况下,得到字典中的第二个元素 - {dave:2}

How do I get the 2nd element in the dictionary - {dave:2} in this case?

背景:我已经看到这个问题在SO上以多种形式出现在
中,所以我以为我会写一个Q& A页面作为社区wiki,人们可以
参考,可能希望成为这个问题的规范答案。

Background: I've seen this question asked so many times on SO in one form or another, so I thought I'd write a Q&A page as a community wiki that folks can be referred to and which might hopefully become the canonical answer for this question.

这个Q& A适用于字典,因为它们在许多不同的
语言。不同的语言使用不同的名称来引用什么是
本质上是相同的数据结构 - 例如它们在
Perl中被称为散列,Python中的字典。在Objective-C中,它们是
NSDictionary NSMutableDictionary 类的实例。

This Q&A applies to dictionaries as they are implemented in many different languages. Different languages use different names to refer to what is essentially the same data structure - for example they're called hashes in Perl, dictionaries in Python. In Objective-C they're instances of the NSDictionary or NSMutableDictionary classes.

推荐答案

简而言之,你不能 - 因为字典是无序的
键/值对的集合。您填充字典
的顺序不会保留在内存中。以下是Python中的一个简单示例:

In a nutshell, you can't - because dictionaries are unordered collections of key/value pairs. The order in which you populate a dictionary is not retained in memory. Here's a simple example in Python:

>>> dict = { 'a': 1, 'b': 2, 'c': 3 }
>>> dict # show the value of dict in memory
{'a': 1, 'c': 3, 'b': 2}

正如你所看到的,虽然字典用
中的键初始化,但是命令a,b,c,打印字典的值显示为
,c,b。即使订单不保留在内存中;
当你在字典中添加更多的键/值对时,
以上表达式的顺序将继续更改。

As you can see, although the dictionary is initialised with keys in the order a, b, c, printing the value of the dictionary shows them ordered a, c, b. Even that ordering is not retained in memory; as you add more key/value pairs to a dictionary the ordering in the above expression will continue to change.

字典经过优化,可根据唯一的密钥快速存储和检索
值。实现从一种语言到
接下来,但通常它的工作原理如下:

Dictionaries are optimised for fast storage and retrieval of a value based on a unique key. The implementation varies from one language to the next but typically it works something like this:


  • 字典支持标准存储一个键/值对的N个元素数组

  • ,该键通过一个函数放入
    将其转换为整数(哈希函数 - 因此名称
    哈希为Perl中的数据结构)。 ASCII字符串键的简单散列函数
    可能是将每个
    字符的ASCII值相加。 (注意:这不是一个好的哈希算法 - 只是一个简单的
    示例!)

  • 然后将整数除以数组,其余的
    用作数组

  • 的索引,如果该索引的数组元素已经填充,然后
    使用各种技术来解决冲突(例如
    ,数组元素的内容本身就是一个数组,
    的新值以及其未标注的键被附加)

  • 检索的工作方式与存储相同:取密钥,使用它
    派生数组索引,然后检索与该
    密钥相关联的值

  • 背景数组可以随着字典的增长而调整大小:在调整大小
    数组时,字典中的每个元素的散列值除以
    新的数组大小,导致新的余数,即新的位置
    在支持数组。

  • the dictionary is backed by a standard array of N elements
  • on storing a key/value pair, the key is put through a function that transforms it into an integer (a "hashing function" - hence the name "hash" for this data structure in Perl). A simple hashing function for ASCII string keys might be to add up the ASCII values of each character. (Note: that's not a good hashing algorithm - just an example of a simple one!)
  • the integer is then divided by the size of the array, and the remainder of that division is used as an index into the array
  • if the array element at that index is already populated then one of a variety of techniques is used to resolve the clash (e.g. the contents of the array element are themselves an array to which the new value along with its unhashed key are appended)
  • retrieval works in the same way as storage: take the key, use it to derive an array index, then retrieve the value associated with that key
  • the backing array can be resized as the dictionary grows: on resizing the array, each element in the dictionary has its hash value divided by the new array size, resulting in a new remainder, i.e. a new location in the backing array.

这篇关于如何访问字典或散列中的第n项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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