首页 > 解决方案 > python中的哈希映射实现

问题描述

所以我试图dict从头开始考虑碰撞,我也通过单独的链接处理。我从文件中添加了一些键值对.csv来计算出现次数最多的单词。但是我很难将值相加,因为尽管处理了这个,但它在同一个索引中多次存储相同的键(参见代码hash_table[word] += 1.....)。我试过调试仍然无法查明问题所在。

class HashMap:
    def __init__(self):
        self.max = 50
        self.array = [[] for item in range(self.max)]

    
    def hash(self, key):
        hash_values = 0
        if len(key) == 1:
            hash_values = ord(key)
        else:
            for char in key:
                hash_values += ord(char)
        
        return hash_values % 50

    
    def __setitem__(self, key, values):
        hash = self.hash(key)
        for idx, item in enumerate(self.array[hash]):
            if len(item) == 2 and item[0] == key:
                self.array[hash][idx][1] =+ 1
                break
        self.array[hash].append([key, values])

    
    def __getitem__(self, key):
        h = self.hash(key)
        for idx, item in enumerate(self.array[h]):
            if item[0] == key:
                return item[1]
              

    def __str__(self):
        return f"{self.array}"

hash_table = HashMap()

word_dict = []
with open("poem.csv", "r") as csv_file:
    for line in csv_file:
        words = line.split(" ")
        for word in words:
            word = word.replace('\n','')
            if hash_table[word]:
                hash_table[word] += 1
                break
            hash_table[word] = 1

.csv:

Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

and

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same,

输出:

[[['And', 1], ['sorry', 1], ['And', 2], ['And', 2], ['bent', 1], ['', 1], ['', 2], ['And', 2]], [], [], [], [['travel', 1], ['both', 1], ['just', 1], ['worn', 1]], [['them', 1]], [], [['and', 1]], [], [], [['wood,', 1], ['could', 1]], [], [['roads', 1], ['not', 1], ['as', 1], ['as', 2], ['as', 2]]......]

正如您所注意到的,HashMap在同一个索引中多次存储相同的键。

标签: pythondictionaryhashmap

解决方案


你有两个问题。首先,在 中__setitem__,您有 =+ 1。那就是“将此项目的值设置为+1”。你想要+= 1。C 两者都有,Python 只有一个。

其次,在 中__setitem__,如果您找到该项目,则修改该条目,然后您break退出循环,然后将另一个项目附加到列表中。你应该return,不是break

通过这两个修复,您的代码对我有用。


推荐阅读