python 散列表查找
Posted F
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 散列表查找相关的知识,希望对你有一定的参考价值。
class HashTable: def __init__(self, size): self.elem = [None for i in range(size)] self.count = size # def hash(self, key): return key % self.count # def insert_hash(self, key): address = self.hash(key) # while self.elem[address]: # address = (address+1) % self.count # self.elem[address] = key # def search_hash(self, key): star = address = self.hash(key) while self.elem[address] != key: address = (address + 1) % self.count if not self.elem[address] or address == star: # return False return True list_a = [12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34] hash_table = HashTable(12) for i in list_a: hash_table.insert_hash(i) for i in hash_table.elem: if i: print((i, hash_table.elem.index(i)), end=" ") print("n") print(hash_table.search_hash(15)) print(hash_table.search_hash(33))
以上是关于python 散列表查找的主要内容,如果未能解决你的问题,请参考以下文章