为什么列表中的某些元素返回无值?

时间:2019-01-18 01:51:01

标签: python list dictionary hash

我有一个花朵定义列表,列表中包含每朵花朵及其含义。由于某种原因,每当我寻找“玫瑰”或“紫藤”时,它都表示它们不在列表中。基本上,只要搜索到的花朵不在列表中,它就会返回我定义的字符串。

我正在使用哈希图。

如果我将“玫瑰”更改为“玫瑰”,则该花适用于该花。这是为什么?我认为可能是因为玫瑰一词太短了,没有任何意义,但我正在尝试研究所有可能性。 “紫藤”不短,并且输出相同。

为了方便学习,我在此代码块中包含了linked_list.py和Blossom_lib.py的代码。

from linked_list import Node, LinkedList
from blossom_lib import flower_definitions

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

    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)
            current_node = next_node

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

class HashMap:
    def __init__(self, size):
        self.array_size = size
        self.array = [LinkedList() for i in range(self.array_size)]

    def hash(self, key):
        key_bytes = key.encode()
        hash_code = sum(key_bytes)
        return hash_code

    def compressor(self, hash_code):
        return hash_code % self.array_size

    def assign(self, key, value):
        hash_code = self.hash(key)
        array_index = self.compressor(hash_code)
        payload = Node([key, value])
        list_at_array = self.array[array_index]

        for list in list_at_array:
            if list[0] == key:
                list[1] = value

        list_at_array.insert(payload)

    def retrieve(self, key):
        hash_code = self.hash(key)
        array_index = self.compressor(hash_code)
        list_at_index = self.array[array_index]

        for list in list_at_index:
            if list[0] == key:
                return "\n" + list[0] + " means " + list[1]
            else:
                return "\n" + key + " not found in list!"

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'], ['roses', 'love'], ['snapdragon', 'grace'], ['sunflower', 'longevity'],
                      ['wisteria', 'good luck']]

blossom = HashMap(len(flower_definitions))

for flower in flower_definitions:
    blossom.assign(flower[0], flower[1])
    print(flower[0])

print(str(blossom.retrieve('wisteria')))

0 个答案:

没有答案