了解词典 [英] Towards understanding dictionaries

查看:181
本文介绍了了解词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用多个哈希表,所以在 c ++ ,通常我会使用 std :: unordered_map 。到目前为止,我可以明白,我可以在Python中使用一个字典,所以让我们假设以下代码:

I am required to use multiple hashtables, so in c++, I would normally use an std::unordered_map. So far I can understand that I can use a dictionary in Python, so let's assume the following code:

my_dict_1 = {}
my_dict_1['foo'] = 1
my_dict_2 = {}
my_dict_2['foo'] = 2

两个字典将使用不同的哈希函数(注意密钥相同),因此它们可以被认为是两个不同的哈希表(我的意思是他们实际上会存储数据不同)?

Will the two dictionaries be using different hash functions (notice that the key is the same), thus they can be considered two different hash tables (I mean that they will actually store the data differently)?

编辑:

是的,字典当然是两个不同的对象,但问题是他们将用于存储数据的技术!

Yes the dictionaries are two different objects of course, but the question is about the technique that they will use to store the data!

推荐答案

p>一个简单的Python shell实验,以显示不同的字典可以使用相同的键:

A simple Python shell experiment to show that different dictionaries can use the same key:

>>> my_dict_1 = {'foo':1}
>>> my_dict_2 = {'foo':2}
>>> my_dict_1,my_dict_2
({'foo': 1}, {'foo': 2})

这是一个很好的讨论,如何实现。关键是每个字典都分配了自己的内存部分(当然这也可以根据需要增长)。这两个字典都使用完全相同的哈希函数,但正在用于探测内存中的不同区域。

This is a good discussion of how it is implemented. The key point is that each dictionary is allocated its own portion of memory (which can of course grow as needed). The exact same hash function is used for both dictionaries, but is being used to probe different areas in memory.

这篇关于了解词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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