算法——散列表

散列表

算法——散列表

散列表(hash table):键值(key_value)映射,Python提供的哈希列表实现为字典。

作用:
模拟映射关系
便于查找
避免重复
缓存/记住数据,以免服务器再通过处理来生成它们

# hash_table.py 哈希表
# 避免重复


def vote(li):
    voters = {}
    for i in li:
        if i not in voters:
            voters[i] = True
        else:
            print(i + ' has already voted.')

    return voters


if __name__ == '__main__':
    li = ['John', 'Alex', 'Gareth', 'Oda', 'John']
    print(vote(li))
Resistance is Futile!
原文地址:https://www.cnblogs.com/noonjuan/p/10923460.html