美文网首页
python字典存储和取值逻辑粗略认识

python字典存储和取值逻辑粗略认识

作者: DayBreakL | 来源:发表于2020-05-01 23:18 被阅读0次
    dict1={'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'}
    # python3.6之前,散列类型,假设开辟了一个长度为20的空间
    entry=[
        # ["key的哈希值","key","value"],存在下标是"key的哈希值%20"的位置,如果散列冲突,有一个散列冲突的算法
         ["None","None","None"],
        ["None","None","None"],
        ["None","None","None"],
        ["-6012638457009940417","name","mike"],
        ["-4396601579178830235","gender","male"],
        ["None","None","None"],
        ["None","None","None"],
        ["None","None","None"],
        ["None","None","None"],
        ["-8437040987111082449","dob","1999-01-01"],
        ["None","None","None"],
        ...
    ]
    print('gender'.__hash__()) # 示范取一个哈希值,实际逻辑并不是用这个方法取哈希值
    print('gender'.__hash__()%20) 
    # 访问时,根据key的哈希值取余,找到下标,key值是否相同,如果相同,取出value,如果不一样,根据散列冲突时的方案,找到真正的key
    
    # 3.6之后,序列类型,会建两个列表id通过散列存下标,entry通过序列存哈希值、key、value
    # 访问时,根据key的哈希值取余,得到id列表的下标,对应的值就是entry里的下标,找到entry里的元素,取出value
    id=[
        [],[],[0],[1],[],[],[],[],[3],[],...
    ]
    entry=[
        ["-6012638457009940417","name","mike"],
        ["-4396601579178830235","gender","male"],
        ["-8437040987111082449","dob","1999-01-01"],
    ]
    

    相关文章

      网友评论

          本文标题:python字典存储和取值逻辑粗略认识

          本文链接:https://www.haomeiwen.com/subject/qovjyhtx.html