分配/检索方法在单独的链式Python哈希图中不起作用

时间:2018-08-23 02:16:13

标签: python hashmap

尽管我知道Python具有实用的内置字典,但我正在尝试通过创建带有花朵及其定义的散列映射来理解单独的链式散列映射的概念。

但是,当我尝试从哈希图打印检索结果时,我得到了None

from linked_list import Node, LinkedList

class HashMap:
  def __init__(self, size):
    self.array_size = size
    self.array = [LinkedList() for i 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):
    hash_code = self.hash(key)
    array_index = self.compress(hash_code)
    payload = Node([key, value])
    list_at_array = self.array[array_index]
    for item in list_at_array:
      if key == item[0]:
        item[1] = value
      else:
        self.array[array_index].insert(payload)

  def retrieve(self, key):
    hash_code = self.hash(key)
    array_index = self.compress(hash_code)
    list_at_index = self.array[array_index]
    for item in list_at_index:
      if item[0] == key:
        return item[1]
      else:
        return None

from blossom_lib import flower_definitions

blossom = HashMap(len(flower_definitions))

for each in flower_definitions:
  blossom.assign(each[0], each[1])
print(blossom.retrieve('daisy'))

这是两个模块:

blossom_lib.py

flower_definitions = [['begonia', 'cautiousness'], ['chrysanthemum', 'cheerfulness'], ['carnation', 'memories'], ['daisy', 'innocence'], ['hyacinth', 'playfulness'], ['lavender', 'devotion'], ['magnolia', 'dignity'], ['morning glory', 'unrequited love'], ['periwinkle', 'new friendship'], ['poppy', 'rest'], ['rose', 'love'], ['snapdragon', 'grace'], ['sunflower', 'longevity'], ['wisteria', 'good luck']]

linked_list.py

class Node:
  def __init__(self, value):
    self.value = value

  def get_value(self):
    return self.value

  def get_next_node(self):
    return self.next_node

  def set_next_node(self, next_node):
    self.next_node = next_node

class LinkedList:
  def __init__(self, head_node=None):
    self.head_node = head_node

  def insert(self, new_node):
    current_node = self.head_node

    if not current_node:
      self.head_node = new_node

    while(current_node):
      next_node = current_node.get_next_node()
      if not next_node:
        current_node.set_next_node(new_node)

  def __iter__(self):
    current_node = self.head_node
    while(current_node):
      yield current_node.get_value()

谢谢!

1 个答案:

答案 0 :(得分:0)

这里有一些问题。

1)Node的{​​{1}}初始化-导致AttributeError

next_node

2)def __init__(self, value): self.value = value self.next_node = None 迭代- LinkedList控制循环的变量从未移到列表中的下一个元素。

current_node

3)def __iter__(self): current_node = self.head_node while (current_node): value = current_node.get_value() current_node = current_node.get_next_node() yield value LinkedList-与前面的情况类似,insert从未移到列表中的下一个元素。

current_node

4)while (current_node): next_node = current_node.get_next_node() if not next_node: current_node.set_next_node(new_node) current_node = next_node HashMap-过于复杂,在确定要放入的正确列表之后,使用该列表的assign方法就足够了。

insert

5)def assign(self, key, value): hash_code = self.hash(key) array_index = self.compress(hash_code) payload = Node([key, value]) list_at_array = self.array[array_index] list_at_array.insert(payload) HashMap-如果列表包含多个元素(具有相同的哈希值)并且第一个元素不是我们要查找的元素,则它将返回None。正确的方法是在遍历列表中的所有元素后返回retrieve,如果找不到该元素,则返回None,因此解决方法是减少{{1}的缩进}。

None

使用以下方法检查示例的正确性:

return None

礼物:

def retrieve(self, key):
    hash_code = self.hash(key)
    array_index = self.compress(hash_code)
    list_at_index = self.array[array_index]
    for item in list_at_index:
        if item[0] == key:
            return item[1]
    return None

可以找到完整的解决方案here