我的哈希码只返回单词的整个标题。我想让它显示结果,只使用至少 2 个单词(以后)的关键字,然后显示结果(获取函数)。
我的哈希码
class hashin:
def __init__(self):
self.size = 217 # size of hash table
self.map = [None] * self.size
def _get_hash(self, key):
hash = 0
for char in str(key):
hash += ord(char)
return hash % self.size
#returns the ASCII value of char in str(key)
def add(self, key, value): # add item to list
key_hash = self._get_hash(key)
key_value = [key, value]
if self.map[key_hash] is None:
self.map[key_hash] = list([key_value])
return True
else:
for pair in self.map[key_hash]:
if pair[0] == key:
pair[1] = value
return True
self.map[key_hash].append(key_value)
return True
def get(self, key): # search for item
key_hash = self._get_hash(key)
if self.map[key_hash] is not None:
for pair in self.map[key_hash]: # find pair of words
if pair[0] == key: # if pair is equals to the whole title of the word
return pair[0] + " - " + pair[1]
return "Error no results for %s \nEnter the correct word." % (key)
样本输出:
输入整个标题时
输入关键字时(即使输入关键字,我也需要显示结果)
我需要的是:输出:Cheater - Kygos 和他们名字中带有 chea 的其他词