The underlying principles of Python dictionaries
Python Python Python Python Python MD5SHA-1SHA-2NTLMPython hash()Pythonhash().__hash__()
Python Python Python
Python
MD5SHA-1SHA-2NTLM
Python hash()
Pythonhash()
.__hash__().__hash__()
Python hash()
>>> hash(1)1>>> hash(10)10>>> hash(10.00)10>>> hash(10.01)230584300921368586>>> hash(-10.01)-230584300921368586
Pythonhash() 64 Python 3 24
11.01
>>> hash(0.1)230584300921369408>>> hash(230584300921369408)230584300921369408>>> hash(0.1) == hash(230584300921369408)True
number 230584300921369408 number 0.1
Python
>>> hash("Bad Behaviour")7164800052134507161
Python Python 3.3 PYTHONHASHSEED
Python 3.3 DOS Scott Crosby Dan Wallach 2003
DOS DOS Scott Crosby
Python
Python Python
Python TypeError a
>>> hash(["R","e","a","l","P","y","t","h","o","n"])Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unhashable type: 'list'
Python id
>>> class Car():... velocity = 0... direction = 0... damage = 0...>>> first_car = Car()>>> second_car = Car()>>> hash(first_car)274643597>>> hash(second_car)274643604
.__hash__()
Python Python
import pprintclass Hashtable: def __init__(self, elements): self.bucket_size = len(elements) self.buckets = [[] for i in range(self.bucket_size)] self._assign_buckets(elements) def _assign_buckets(self, elements): for key, value in elements: hashed_value = hash(key) index = hashed_value % self.bucket_size self.buckets[index].append((key, value)) def get_value(self, input_key): hashed_value = hash(input_key) index = hashed_value % self.bucket_size bucket = self.buckets[index] for key, value in bucket: if key == input_key: return(value) return None def __str__(self): return pprint.pformat(self.buckets) # here pformat is used to return a printable representation of the objectif __name__ == "__main__": capitals = [ ('France', 'Paris'), ('United States', 'Washington D.C.'), ('Italy', 'Rome'), ('Canada', 'Ottawa') ]hashtable = Hashtable(capitals)print(hashtable)print(f"The capital of Italy is {hashtable.get_value('Italy')}")
for 9 10 11 12
PYTHONHASHSEED46
[[('United States', 'Washington D.C.'), ('Canada', 'Ottawa')], [], [], [('France', 'Paris'), ('Italy', 'Rome')]]The capital of Italy is Rome
PYTHONHASHSEED Python Python 3.3
Python
Python
hash()
```python hl_lines=3 class Hashtable: def init (self, elements): self.bucket_size = len(elements) * 2 self.buckets = [[] for i in range(self.bucket_size)] self._assign_buckets
Running this example, I ended up with a better distribution of the input data, but I had however a collision and five unused buckets:```console[[], [], [], [('Canada', 'Ottawa')], [], [], [('United States', 'Washington D.C.'), ('Italy', 'Rome')], [('France', 'Paris')]]The capital of Italy is Rome
_assign_buckets()
def _assign_buckets(self, elements): self.buckets = [None] * self.bucket_size for key, value in elements: hashed_value = hash(key) index = hashed_value % self.bucket_size while self.buckets[index] is not None: print(f"The key {key} collided with {self.buckets[index]}") index = (index + 1) % self.bucket_size self.buckets[index] = ((key, value))
Nonewhile
get_value()
def get_value(self, input_key): hashed_value = hash(input_key) index = hashed_value % self.bucket_size while self.buckets[index] is not None: key,value = self.buckets[index] if key == input_key: return value index = (index + 1) % self.bucket_size
get_value()None
Italy ( France) Italy
The key Italy collided with ('France', 'Paris')[None, None, ('Canada', 'Ottawa'), None, ('France', 'Paris'), ('Italy', 'Rome'), None, ('United States', 'Washington D.C.')]The capital of Italy is Rome
Italy ( France) FranceItaly
Python
Python Python
>>> chess_players = {... "Carlsen": 2863,... "Caruana": 2835,... "Ding": 2791,... "Nepomniachtchi": 2784,... "Vachier-Lagrave": 2778,... }
chess_players
>>> chess_players["Nepomniachtchi"]2784
Python Key Error
>>> chess_players["Mastromatteo"]Traceback (most recent call last): File "<stdin>", line 1, in <module>KeyError: 'Mastromatteo'
.items()
>>> for (k, v) in chess_players.items():... print(k,v)... Carlsen 2863Caruana 2835Ding 2791Nepomniachtchi 2784Vachier-Lagrave 2778
Python .keys().values()
>>> chess_players.keys()dict_keys(["Carlsen", "Caruana", "Ding", "Nepomniachtchi", "Vachier-Lagrave"])>>> chess_players.values()dict_values([2863, 2835, 2791, 2784, 2778])
>>> chess_players["Grischuk"] = 2777>>> chess_players{'Carlsen': 2863, 'Caruana': 2835, 'Ding': 2791, 'Nepomniachtchi': 2784, 'Vachier-Lagrave': 2778, 'Grischuk': 2777}
hashable TypeError
>>> my_list = ["Giri", "Mamedyarov"]chess_players[my_list] = 2764Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unhashable type: 'list'
del
>>> del chess_players["Grischuk"]>>> chess_players{'Carlsen': 2863, 'Caruana': 2835, 'Ding': 2791, 'Nepomniachtchi': 2784, 'Vachier-Lagrave': 2778}
Python Pythonic
Python Python
Python Python 3.6 Python 3.6 Python 3.7 Guido
PythonPython240
>>> import sys>>> my_dict = {}>>> sys.getsizeof(my_dict)240
Python240
>>> my_dict["a"] = 100>>> sys.getsizeof(my_dict)240
Python 3.6 8 240 Python
>>> for i in range(20):... my_dict[i] = 100... print(f"elements = {i+1} size = {sys.getsizeof(my_dict)}")... elements = 1 size = 240elements = 2 size = 240elements = 3 size = 240elements = 4 size = 240elements = 5 size = 240elements = 6 size = 368elements = 7 size = 368elements = 8 size = 368elements = 9 size = 368elements = 10 size = 368elements = 11 size = 648elements = 12 size = 648elements = 13 size = 648elements = 14 size = 648elements = 15 size = 648elements = 16 size = 648elements = 17 size = 648elements = 18 size = 648elements = 19 size = 648elements = 20 size = 648
Python
>>> keys = list(my_dict.keys())>>> for key in keys:... del my_dict[key]...>>> my_dict{}>>> sys.getsizeof(my_dict)648
.clear() 72
>>> my_dict.clear()>>> sys.getsizeof(my_dict)72
8
Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.(Email:[email protected])