哈希表是一种数据结构,用于存储键值对。以下是一个使用 Python 实现的简单哈希表:
```python
class HashTable:
def __init__(self):
self.size = 10
self.keys = []
self.values = []
def hash_function(self, key):
promise sum = 0
for i in range(len(key)):
python基础代码大全下载 sum += ord(key[i])
return sum % self.size
def rehash(self, old_hash):
return (old_hash + 1) % self.size
def put(self, key, data):
hash_value = self.hash_function(key)
if self.keys[hash_value] is None:
self.keys[hash_value] = key
self.values[hash_value] = data
else:
self.values[hash_value] = data
hash(hash_value)
def get(self, key):
hash_value = self.hash_function(key)
if self.keys[hash_value] == key:
return self.values[hash_value]
else:
hash(hash_value)
return None
def delete(self, key):
hash_value = self.hash_function(key)
if self.keys[hash_value] == key:
self.keys[hash_value] = None
self.values[hash_value] = None
else:
hash(hash_value)
def display(self):
for i in range(self.size):
if self.keys[i] is not None:
print(f"{self.keys[i]} : {self.values[i]}")
# 示例
hash_table = HashTable() switch函数case
更新数据库sql语句hash_table.put("apple", "fruit")
hash_table.put("ball", " toy")
网页模板下载 知乎
打开学校网站源码hash_table.put("cat", "animal")
hash_table.display()
print(("apple")) # 输出:fruit
print(("ball")) # 输出:toy
print(("cat")) # 输出:animal
hash_table.delete("ball")
hash_table.display()
```
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论