class HashMap:
def __init__(self):
self.max_length = 8
self.max_load_factor = 0.8
self.length = 0
self.map = [None] * self.max_length
def get(self, key, default):
value = dict.get(key, default)
return value # returns the value for key if key is in the dictionary, else
# default. If default is not given, it defaults to none.
def set(self, key, value):
#need to add the key value pair into the hashmap
#if self.max_load_factor >= .8:
#refresh the map into a map double the capacity
因此,我可以为我的哈希映射放入get()
方法,这相对简单(如果键在字典中,它会返回键的值,等等(。但是,我如何将键值对添加到哈希映射本身中呢?
有人能给我指正确的方向吗?是否需要将其添加到self.map
实例中?
您没有向我们展示如何使用它,我也看不到您在任何地方定义dict
。但我想我看到了你想要的,还有类似的东西
class HashMap:
def __init__(self):
self.dict = {}
def get(self, key, default=None):
return self.dict.get(key, default)
def set(self, key, value):
self.dict[key] = value
if __name__ == '__main__':
hm = HashMap()
hm.set('test', 'hello, world')
print(hm.get('test'))
应该这样做(并输出(
hello, world