python - 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
在同一个索引中多次存储相同的键。
解决方案
你有两个问题。首先,在 中__setitem__
,您有 =+ 1
。那就是“将此项目的值设置为+1”。你想要+= 1
。C 两者都有,Python 只有一个。
其次,在 中__setitem__
,如果您找到该项目,则修改该条目,然后您break
退出循环,然后将另一个项目附加到列表中。你应该return
,不是break
。
通过这两个修复,您的代码对我有用。
推荐阅读
- sql - 未使用嵌套 JSONB 字段的索引
- loops - 循环 Tukey 测试中变量名的输出
- azure-service-fabric - ServiceFabric 没有在更新时关闭 CommunicationListener?
- android - Oreo - 后台定位服务运动追踪器
- ios - 从服务器上的文本实例化 Swift 类
- javascript - C 函数到 Javascript
- java - shp2pgsql, psql 命令未通过 ProcessBuilder Java 找到
- c# - 如何配置 EF Core 连接上下文以解耦包依赖关系?
- xamarin - Xamarin 项目不支持框架 '.netframework version=v4.0'
- react-native - 反应本机模式未打开