1.拉链法 简单实现: class Dict: def __init__(self, solt_number): self.solt_number = solt_number
1.拉链法
简单实现:
class Dict: def __init__(self, solt_number): self.solt_number = solt_number self.solts = [] for _ in range(solt_number): self.solts.append([]) def put(self, key, value): idx = hash(key) % self.solt_number self.solts[idx].append((key, value)) def get(self, key): idx = hash(key) % self.solt_number for k, v in self.solts[idx]: if k == key: return v raise KeyError(key)myd = Dict(16)for i in range(20): myd.put(str(i), i)