from linked_list import Node,LinkedList
from blossom_lib import flower_definitions
class HashMap():
def __init__(self,size):
self.array_size = size
self.array = [LinkedList() for number in range(size)]
def hash(self,key):
return sum(key.encode())
def compress(self,hash_code):
return hash_code % self.array_size
def assign(self,key,value):
array_index=self.compress(self.hash(key))
payload = Node([key,value])
#self.array[array_index] = [key,value]
list_at_array = self.array[array_index]
for node in list_at_array:
if node.value[0] == key: #as node has the above definition of payload = Node([key,value]) it basically says we have a node with value = [key,value]
node.value[1] = value
list_at_array.insert(payload)
def retrieve(self,key):
array_index = self.compress(self.hash(key))
list_at_index = self.array[array_index]
for node in list_at_index:
if node.value[0] == key:
return node.value[1]
else:
return None
if payload[0] == key:
return payload[1]
elif payload[0] == None or payload[0] !=key:
return None
blossom = HashMap(len(flower_definitions))
for flower in flower_definitions:
blossom.assign(flower[0],flower[1])
print(flower_definitions)
输出:
错误:LinkedList 没有 value 属性。
LinkedList
是一个链表类,用于存储 Node 类的不同节点,即 Node(value)
根据节点定义,我们有以下构造函数:
self.value = value
...而 value
是键值对,即 [key,value]
。
但是,它告诉我链表中的项目(在我看来项目 = Node 类的实例)没有 value 属性。
我的逻辑:
list_at_array = self.array[array_index]
... where array_index
标记数组中添加键值对的位置。该数组是许多 LinkedLists - 其项目是节点 - 具有存储在 flower_definitions
中的所有花的大小。因此,list_at_array
选择所选 LinkedList
对象的第一个条目 - 根据定义为节点。
如果我们现在决定向 LinkedList
添加一个名为 list_at_array
的键值对,那么我们基本上是告诉 LinkedList
对象表现得像一个普通列表?>