【发布时间】:2019-03-25 11:55:50
【问题描述】:
我正在尝试在 Python 中实现多重映射。我有每条记录的三个字段。
序列号、名称、食物
1 John Apple
2 Bill Orange
3 Josh Apple
这里,SerialNo 和 Name 不会重复,Food 除外。
我可以在我的哈希图中插入一个键、值和查询。但是,如何与三个值建立关系。作为,我想查询,
SerialNo s where Food='Apple'
Name where Food='Apple'
Food where Name='Bill'
Get the all stored data (SerialNo, Name, Food)
我只能创建一个索引,但是如何查询每个字段。
这是我插入数据的 hashmap,
class HashMap:
def __init__(self):
self.store = [None for _ in range(16)]
self.size = 0
def put(self, key, value):
p = Node(key, value)
key_hash = self._hash(key)
index = self._position(key_hash)
if not self.store[index]:
self.store[index] = [p]
self.size += 1
else:
list_at_index = self.store[index]
if p not in list_at_index:
list_at_index.append(p)
self.size += 1
else:
for i in list_at_index:
if i == p:
i.value = value
break
我不能使用 dict ,我更喜欢从头开始构建函数以用于学习目的。
【问题讨论】:
标签: python algorithm data-structures hashmap