当一个值被散列为相同的值时,它将被添加到该散列值引用的链接列表中。为什么哈希表的实现将数组上的链表用作存储桶?
是因为数组在初始化时具有预定的大小,所以在存储桶中添加太多元素时需要调整大小吗?
无论我在哪里搜索,他们都说python词典没有任何顺序.当我运行代码1时,每次显示不同的输出(随机顺序).但是当我运行代码2时,它总是显示相同的排序输出.为什么字典在第二个片段中排序?
#code 1
d = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
for a, b in d.items():
print(a, b)
#code 2
d = {1: 10, 2: 20, 3: 30, 4: 40}
for a, b in d.items():
print(a, b)
Run Code Online (Sandbox Code Playgroud)
输出
代码1:
four 4
two 2
three 3
one 1
Run Code Online (Sandbox Code Playgroud)
代码1:
three 3
one 1
two 2
four 4
Run Code Online (Sandbox Code Playgroud)
代码2(总是):
1 10
2 20
3 30
4 40
Run Code Online (Sandbox Code Playgroud) 我对想澄清的字典和哈希表有些困惑,假设我有当前的字典以及当前python运行的哈希值的当前输出。
Dict = dict()
print(hash('a'))
print(hash('b'))
print(hash('c'))
Dict['a'] = 1
Dict['b'] = 2
Dict['c'] = 3
print(Dict)
Run Code Online (Sandbox Code Playgroud)
具有的输出
1714333803
1519074822
1245896149
{'a': 1, 'c': 3, 'b': 2}
Run Code Online (Sandbox Code Playgroud)
因此,据我所知,哈希表只是一个数组,其中哈希是哈希表的索引。例如,“ a”的哈希值为1714333803,因此我的哈希表索引1714333803的值为“ a”。因此,我感到困惑的是,哈希表有多少个索引以及哈希函数如何产生答案?它使用模数并且具有固定范围的索引吗?因为给定的字典打印结果输出{'a': 1, 'c': 3, 'b': 2},但是即使假设它输出也正确,但实际上该字典实际上是至少1714333803索引的数组,因为包含3个元素似乎太可笑了,更不用说浪费了多少的空间。同样对于散列表,索引中没有值的内容为null吗?
我开始学习 C 语言中的哈希表数据结构,并且我注意到(如果我理解正确的话)哈希表与 Python 字典非常相似。如果我不正确并且它们是两个完全不同的东西,有人可以向我解释哈希表是什么而不会太技术化吗?谢谢。
Python如何存储dict键,在哈希表中发生冲突时的值?什么是用于获取哈希值的哈希算法?
我正在使用结构。有没有办法遍历所有“数字”类型的项目?
struct number { int value; string name; };
Run Code Online (Sandbox Code Playgroud) 如果我运行以下代码:
a={}
a[input("key: ")] = input("value: ")
Run Code Online (Sandbox Code Playgroud)
口译员首先提示我a value input然后是key input.
这背后的原因是什么?
我想只检索词典"e"中的第四项(下面).
我尝试使用OrderedDict()方法,但它没有用.这是我的结果:
from collections import OrderedDict
e = OrderedDict()
e = {'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E'
}
for k, v in e.items():
print k, v
print e.items()[3]
Run Code Online (Sandbox Code Playgroud)
最后一行返回:('e','E')
所以我将键和值转换为列表,但是这些列表在我打印时是如何出现的:
['a', 'c', 'b', 'e', 'd']
['A', 'C', 'B', 'E', 'D']
Run Code Online (Sandbox Code Playgroud)
对我来说,这解释了它为什么会发生,但不是如何发生的.
所以,接下来我整理了它们.这给了我正在寻找的结果 - 但它似乎不必要地复杂化:
e = {'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E'
}
k, v = sorted(e.keys()), sorted(e.values())
print "{}: {}".format(k[3], v[3])
Run Code Online (Sandbox Code Playgroud)
结果:d:D
OrderedDict()不是必需的.
有更简单的方法吗?有人可以解释为什么字典中的元素是这样排序的:
keys: 'a', 'c', 'b', 'e', 'd'
values: …Run Code Online (Sandbox Code Playgroud) 为什么Python中允许使用嵌套字典,而不允许使用嵌套集?
人们可以嵌套字典并动态更改子字典,如下所示:
In [1]: dict1 = {'x':{'a':1, 'b':2}, 'y':{'c':3}}
In [2]: dict2 = {'x':{'a':1, 'b':2}, 'y':{'c':3}}
In [3]: dict1 == dict2
Out[3]: True
In [4]: dict2['x'] = {'d':4}
In [5]: dict1 == dict2
Out[5]: False
Run Code Online (Sandbox Code Playgroud)
另一方面,如果你试图在一个集合中放置一个集合,你会收到一个错误,说它无法完成,因为集合是一个"不可用的类型":
In [6]: set([set(['a'])])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-8e7d044eec15> in <module>()
----> 1 set([set(['a'])])
TypeError: unhashable type: 'set'
Run Code Online (Sandbox Code Playgroud)
但这也没有意义,因为字典也是不可用的,
In [7]: hash({'a':1})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-44def9788331> in <module>()
----> 1 hash({'a':1})
TypeError: unhashable type: 'dict'
Run Code Online (Sandbox Code Playgroud)
当然,可以将一个冷冻集放在一个集合中, …
我已经看到了建议的解决方案和解决方法,但是找不到关于不允许在迭代过程中更改集的选择的解释。你能帮我理解为什么可以吗
In [1]: l = [1]
In [2]: for i in l:
l.append(2*i)
if len(l)>10:
break
In [3]: l
Out[3]: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
Run Code Online (Sandbox Code Playgroud)
虽然这不行
In [4]: l = {1}
In [5]: for i in l:
l.add(2*i)
if len(l)>10:
break
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-5-b5bdff4a382b> in <module>()
----> 1 for i in l:
2 l.add(2*i)
3 if len(l)>10:
4 break
5
RuntimeError: Set changed size during iteration
Run Code Online (Sandbox Code Playgroud)
在迭代时更改集合有什么不好?
我知道集合中的顺序未定义,因此next …