hash table在python中的实现

hash table:又称为散列表,有键和值组成,数组中方商品的价格。
散列表总是将同样的输入映射到形同的索引
散列函数将不同的输入映射到不同的索引
散列函数知道数组有多大,只返回有效的索引。

# 投票可以使用散列表
voted={}
def check_voter(name):
    if voted.get(name):
        print("kick them out")
    else:
        # 此处可以直接利用ture or false来表示是否投票的
        voted[name]=True
        print("let them vote")
check_voter("chenxin")
# 将散列表用作缓存
cache={}
def get_page(url):
    if cache.get(url):
        return cache[url]
    else:
        data=get_data_from_server(url)
        cache[url]=data
        return data

小节:
散列表适合用于
模拟映射关系
防止重复
缓存/记住数据,以免服务器在通过处理来生成它们。

猜你喜欢

转载自blog.csdn.net/lc574260570/article/details/82049681