• 技术文章 >Python技术 >Python基础教程

    python哈希散列的映射

    小妮浅浅小妮浅浅2021-04-30 14:51:42原创2347

    1、散列的映射

    Map()创建一个空映射,然后回到一个空映射集合。

    在put(key,val)的映射中添加新的键值对。若键已存在,则用新值代替旧值。

    get返回key对应的值。如果key不存在,返回none。

    del通过del map[key]语句从映射中删除键-值对。

    len()回到映射中存储的键-值对的数目。

    当键存在时,in通过keyinmap等语句返回True,否则返回False。

    2、实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    class Map(object):

        def __init__(self,size=11):

            self.size = size

            self.__slots = [None] * self.size

            self.__data = [None] * self.size

      

        def put(self, key, val):

            hashvalue = self.hashfunction(key, len(self.__slots))

            if self.__slots[hashvalue] == None:

                self.__slots[hashvalue] = key

                self.__data[hashvalue] = val

            else:

                if self.__slots[hashvalue] == key:

                    self.__data[hashvalue] = val

                else:

                    nextslot = self.rehash(hashvalue, len(self.__slots))

                    while self.__slots[nextslot] != None and self.__slots[nextslot] != key:

                        nextslot = self.rehash(nextslot, len(self.__slots))

                    if self.__slots[nextslot] == None:

                        self.__slots[nextslot] = key

                        self.__data[nextslot] = val

                    else:

                        self.__data[nextslot] = val

      

        def get(self, key):

            startslot = self.hashfunction(key, len(self.__slots))

            data = None

            stop = False

            found = False

            position = startslot

            while self.__slots[position] != None and \

                    not found and not stop:

                if self.__slots[position] == key:

                    found = True

                    data = self.__data[position]

                else:

                    position = self.rehash(position, len(self.__slots))

                if position == startslot:

                    stop = True

            return data

        def delete(self,key):

            pass

        def __getitem__(self, key):

            return self.get(key)

      

        def __setitem__(self, key, val):

            self.put(key, val)

        def __delitem__(self, key):

            self.delete(key)

      

        def len(self):

            pass

      

        def hashfunction(self, key, size):

            return key % size

      

        def rehash(self, oldhash, size):

            return (oldhash + 1) % size

    以上就是python哈希散列的映射,希望对大家有所帮助。更多Python学习指路:python基础教程

    专题推荐:python哈希散列
    上一篇:python二分查找的原理 下一篇:python中树有哪些种类

    相关文章推荐

    • python线程安全的介绍及解决方法• python迭代器协议支持的两种方法• python中chardet库的安装和导入• python chardet库的函数用法• python中使用动量交易策略• python动量交易策略的四个步骤• python time库有哪些时钟• python time.ctime()如何做时间加减法• python strftime获取当前时间• python mktime()如何计算时间• python数据模块类如何定义• python如何定义索引模块类• python搜索模块如何查询• python PyQt5如何实现窗口功能• python阻塞调度如何使用• python标记清除的过程• python单向链表如何实现• python双向链表的概念介绍• Python单向循环链表的创建

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网