您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

用Python实现数据结构之映射

5b51 2022/1/14 8:24:16 python 字数 87058 阅读 596 来源 www.jb51.cc/python

映射与字典 字典dict是Python中重要的数据结构,在字典中,每一个键都对应一个值,其中键与值的关系就叫做映射,也可以说是每一个键都映射到一个值上。 映射(map)是更具一般性的数据类型,具体到P

概述

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10350475"&gt;
<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">映射与字典
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">字典dict是Python中重要的数据结构,在字典中,每一个键都对应一个值,其中键与值的关系就叫做映射,也可以说是每一个键都映射到一个值上。

 collections  MutableMapping

<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class <span class="hljs-title" style="font-weight: bold; color: #0048ab;">MyMap<span class="hljs-params">(MutableMapping):

<span class="hljs-class"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;item</span><span class="hljs-params"&gt;()</span>:</span>

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,key,value)</span>:</span>
        self.key = key
        self.value = value

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__eq__</span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key == other.key

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__ne__</span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key != other.key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self)</span>:</span>
    self.table = []

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__getitem__</span><span class="hljs-params"&gt;(self,item)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == item:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> i.value
    <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(item))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__setitem__</span><span class="hljs-params"&gt;(self,value)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == key:
            i.value = value
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span>
    self.table.append(self.item(key,value))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__delitem__</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> n,i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> enumerate(self.table):
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == key:
            self.pop(n)
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(key))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__len__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> len(self.table)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> i.key

<span class="hljs-class"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;item</span><span class="hljs-params"&gt;()</span>:</span>

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,key,value)</span>:</span>
        self.key = key
        self.value = value

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__eq__</span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key == other.key

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__ne__</span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key != other.key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self)</span>:</span>
    self.table = []

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__getitem__</span><span class="hljs-params"&gt;(self,item)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == item:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> i.value
    <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(item))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__setitem__</span><span class="hljs-params"&gt;(self,value)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == key:
            i.value = value
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span>
    self.table.append(self.item(key,value))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__delitem__</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> n,i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> enumerate(self.table):
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == key:
            self.pop(n)
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(key))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__len__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> len(self.table)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> i.key

<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class <span class="hljs-title" style="font-weight: bold; color: #0048ab;">MyMap<span class="hljs-params">(MutableMapping):

<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class <span class="hljs-title" style="font-weight: bold; color: #0048ab;">MyMap<span class="hljs-params">(MutableMapping):

<span class="hljs-class"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;item</span><span class="hljs-params"&gt;()</span>:</span>

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,key,value)</span>:</span>
        self.key = key
        self.value = value

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__eq__</span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key == other.key

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__ne__</span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key != other.key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self)</span>:</span>
    self.table = []

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__getitem__</span><span class="hljs-params"&gt;(self,item)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == item:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> i.value
    <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(item))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__setitem__</span><span class="hljs-params"&gt;(self,value)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == key:
            i.value = value
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span>
    self.table.append(self.item(key,value))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__delitem__</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> n,i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> enumerate(self.table):
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> i.key == key:
            self.pop(n)
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(key))

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__len__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> len(self.table)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> i <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self.table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> i.key

 :
    mask = ( << ) - 
    h = 
     character  s:
        h = (h <<  & mask) | (h >> )
        h += ord(character)
     h

>是右移,&是按位与,|是按位或,ord()函数返回一个字符的ascii码或unicode值

函数,传入对象x,返回一个整型值作为它的哈希码

自定义的对象作为参数传入则会报错。

自定义的对象能够使用,可以在类中实现一个叫做的特殊方法,在该函数调用hash函数,并传入该对象的一些不可变属性组合,将值再返回,例如:

 :

  <span class="hljs-keyword" style="font-weight: bold;">return hash((self.red,self.green,self.blue))

  <span class="hljs-keyword" style="font-weight: bold;">return hash((self.red,self.green,self.blue))

  <span class="hljs-keyword" style="font-weight: bold;">return hash((self.red,self.green,self.blue))

 :
    
<span class="hljs-class"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;item</span><span class="hljs-params"&gt;()</span>:</span>

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key != other.key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,cap=<span class="hljs-number"&gt;11</span>,p=<span class="hljs-number"&gt;109345121</span>)</span>:</span>
    self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>]
    self._n = <span class="hljs-number"&gt;0</span>           <span class="hljs-comment" style="color: #738191;"&gt;# 元素<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" class="keywords">数量</a></span>
    self._prime = p       <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>
    self._scale = <span class="hljs-number"&gt;1</span> + random.randrange(p+<span class="hljs-number"&gt;1</span>)    <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>
    self._shift = random.randrange(p)    <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_hash_func</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (hash(key)*self._scale+self._shift)<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>elf._prime%len(self._table)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__len__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._n

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__getitem__</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    j = self._hash_func(k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._inner_getitem(j,k)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__setitem__</span><span class="hljs-params"&gt;(self,value)</span>:</span>
    j = self._hash_func(key)
    self._inner_setitem(j,value)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._n>len(self._table)//<span class="hljs-number"&gt;2</span>:          <span class="hljs-comment" style="color: #738191;"&gt;#调整大小,使<a href="https://www.jb51.cc/tag/fuzai/" target="_blank" class="keywords">负载</a>因子小于等于0.5</span>
        self._resize(<span class="hljs-number"&gt;2</span>*len(self._table)-<span class="hljs-number"&gt;1</span>)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__delitem__</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    j = self._hash_func(key)
    self._inner_delitem(j,key)
    self._n -= <span class="hljs-number"&gt;1</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_resize</span><span class="hljs-params"&gt;(self,cap)</span>:</span>
    old = list(self.items())
    self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>]
    self._n = <span class="hljs-number"&gt;0</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> (k,v) <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> old:
        self[k] = v

<span class="hljs-class"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;item</span><span class="hljs-params"&gt;()</span>:</span>

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key != other.key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,cap=<span class="hljs-number"&gt;11</span>,p=<span class="hljs-number"&gt;109345121</span>)</span>:</span>
    self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>]
    self._n = <span class="hljs-number"&gt;0</span>           <span class="hljs-comment" style="color: #738191;"&gt;# 元素<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" class="keywords">数量</a></span>
    self._prime = p       <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>
    self._scale = <span class="hljs-number"&gt;1</span> + random.randrange(p+<span class="hljs-number"&gt;1</span>)    <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>
    self._shift = random.randrange(p)    <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_hash_func</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (hash(key)*self._scale+self._shift)<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>elf._prime%len(self._table)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__len__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._n

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__getitem__</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    j = self._hash_func(k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._inner_getitem(j,k)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__setitem__</span><span class="hljs-params"&gt;(self,value)</span>:</span>
    j = self._hash_func(key)
    self._inner_setitem(j,value)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._n>len(self._table)//<span class="hljs-number"&gt;2</span>:          <span class="hljs-comment" style="color: #738191;"&gt;#调整大小,使<a href="https://www.jb51.cc/tag/fuzai/" target="_blank" class="keywords">负载</a>因子小于等于0.5</span>
        self._resize(<span class="hljs-number"&gt;2</span>*len(self._table)-<span class="hljs-number"&gt;1</span>)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__delitem__</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    j = self._hash_func(key)
    self._inner_delitem(j,key)
    self._n -= <span class="hljs-number"&gt;1</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_resize</span><span class="hljs-params"&gt;(self,cap)</span>:</span>
    old = list(self.items())
    self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>]
    self._n = <span class="hljs-number"&gt;0</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> (k,v) <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> old:
        self[k] = v

<span class="hljs-class"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;item</span><span class="hljs-params"&gt;()</span>:</span>

    <span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,other)</span>:</span>
        <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self.key != other.key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params"&gt;(self,cap=<span class="hljs-number"&gt;11</span>,p=<span class="hljs-number"&gt;109345121</span>)</span>:</span>
    self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>]
    self._n = <span class="hljs-number"&gt;0</span>           <span class="hljs-comment" style="color: #738191;"&gt;# 元素<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" class="keywords">数量</a></span>
    self._prime = p       <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>
    self._scale = <span class="hljs-number"&gt;1</span> + random.randrange(p+<span class="hljs-number"&gt;1</span>)    <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>
    self._shift = random.randrange(p)    <span class="hljs-comment" style="color: #738191;"&gt;# MAD中的参数</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_hash_func</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (hash(key)*self._scale+self._shift)<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>elf._prime%len(self._table)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__len__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._n

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__getitem__</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    j = self._hash_func(k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._inner_getitem(j,k)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__setitem__</span><span class="hljs-params"&gt;(self,value)</span>:</span>
    j = self._hash_func(key)
    self._inner_setitem(j,value)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._n>len(self._table)//<span class="hljs-number"&gt;2</span>:          <span class="hljs-comment" style="color: #738191;"&gt;#调整大小,使<a href="https://www.jb51.cc/tag/fuzai/" target="_blank" class="keywords">负载</a>因子小于等于0.5</span>
        self._resize(<span class="hljs-number"&gt;2</span>*len(self._table)-<span class="hljs-number"&gt;1</span>)

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__delitem__</span><span class="hljs-params"&gt;(self,key)</span>:</span>
    j = self._hash_func(key)
    self._inner_delitem(j,key)
    self._n -= <span class="hljs-number"&gt;1</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_resize</span><span class="hljs-params"&gt;(self,cap)</span>:</span>
    old = list(self.items())
    self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>]
    self._n = <span class="hljs-number"&gt;0</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> (k,v) <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> old:
        self[k] = v

 :
    解决冲突的方式实现的哈希表"""
<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_getitem</span><span class="hljs-params"&gt;(self,j,k)</span>:</span>
    bucket = self._table[j]            <span class="hljs-comment" style="color: #738191;"&gt;#把二级容器叫做桶</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> bucket[k]

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_setitem</span><span class="hljs-params"&gt;(self,k,v)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        self._table[j] = MyMap()
    oldsize = len(self._table[j])
    self._table[j][k] = v
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> len(self._table[j])>oldsize:
        self._n += <span class="hljs-number"&gt;1</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_delitem</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    bucket = self._table[j]
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;del</span> bucket[k]

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self._table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> key <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> bucket:
                <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_getitem</span><span class="hljs-params"&gt;(self,j,k)</span>:</span>
    bucket = self._table[j]            <span class="hljs-comment" style="color: #738191;"&gt;#把二级容器叫做桶</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> bucket[k]

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_setitem</span><span class="hljs-params"&gt;(self,k,v)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        self._table[j] = MyMap()
    oldsize = len(self._table[j])
    self._table[j][k] = v
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> len(self._table[j])>oldsize:
        self._n += <span class="hljs-number"&gt;1</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_delitem</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    bucket = self._table[j]
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;del</span> bucket[k]

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self._table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> key <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> bucket:
                <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_getitem</span><span class="hljs-params"&gt;(self,j,k)</span>:</span>
    bucket = self._table[j]            <span class="hljs-comment" style="color: #738191;"&gt;#把二级容器叫做桶</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span>+ repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> bucket[k]

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_setitem</span><span class="hljs-params"&gt;(self,k,v)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        self._table[j] = MyMap()
    oldsize = len(self._table[j])
    self._table[j][k] = v
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> len(self._table[j])>oldsize:
        self._n += <span class="hljs-number"&gt;1</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_delitem</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    bucket = self._table[j]
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;del</span> bucket[k]

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> self._table:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> bucket <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> key <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> bucket:
                <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> key

 :
    解决冲突实现的哈希表"""
    _AVAIL = object()  标记删除位置
<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_is_available</span><span class="hljs-params"&gt;(self,j)</span>:</span>
    <span class="hljs-string" style="color: #0048ab;"&gt;"""判断该位置是否可用"""</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;or</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> HashMapTwo._AVAIL

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_find_slot</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    <span class="hljs-string" style="color: #0048ab;"&gt;"""寻找键k所在的索引
       如果找到了,返回(True,索引)
       如果没找到,返回(False,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的索引位置)"""</span>

    firstAvail = <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;while</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;True</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._is_available(j):
            <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> firstAvail <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:  <span class="hljs-comment" style="color: #738191;"&gt;# _AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>可以是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
                firstAvail = j
            <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:  <span class="hljs-comment" style="color: #738191;"&gt;# 跳过_AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
                <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (<span class="hljs-keyword" style="font-weight: bold;"&gt;False</span>,firstAvail)
        <span class="hljs-keyword" style="font-weight: bold;"&gt;elif</span> k == self._table[j].key:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (<span class="hljs-keyword" style="font-weight: bold;"&gt;True</span>,j)
        j = (j + <span class="hljs-number"&gt;1</span>) % len(self._table)  <span class="hljs-comment" style="color: #738191;"&gt;# 向下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>查找</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_getitem</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    found,s = self._find_slot(j,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._table[s].value

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_setitem</span><span class="hljs-params"&gt;(self,v)</span>:</span>
    found,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:  <span class="hljs-comment" style="color: #738191;"&gt;# 使用第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
        self._table[s] = self.Item(k,v)
        self._n += <span class="hljs-number"&gt;1</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;else</span>:
        self._table[s].value = v

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_delitem</span><span class="hljs-params"&gt;(self,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    self._table[s] = HashMapTwo._AVAIL  <span class="hljs-comment" style="color: #738191;"&gt;# <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> j <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> range(len(self._table)):
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> self._is_available(j):
            <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> self._table[j].key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_is_available</span><span class="hljs-params"&gt;(self,j)</span>:</span>
    <span class="hljs-string" style="color: #0048ab;"&gt;"""判断该位置是否可用"""</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;or</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> HashMapTwo._AVAIL

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_find_slot</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    <span class="hljs-string" style="color: #0048ab;"&gt;"""寻找键k所在的索引
       如果找到了,返回(True,索引)
       如果没找到,返回(False,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的索引位置)"""</span>

    firstAvail = <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;while</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;True</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._is_available(j):
            <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> firstAvail <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:  <span class="hljs-comment" style="color: #738191;"&gt;# _AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>可以是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
                firstAvail = j
            <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:  <span class="hljs-comment" style="color: #738191;"&gt;# 跳过_AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
                <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (<span class="hljs-keyword" style="font-weight: bold;"&gt;False</span>,firstAvail)
        <span class="hljs-keyword" style="font-weight: bold;"&gt;elif</span> k == self._table[j].key:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (<span class="hljs-keyword" style="font-weight: bold;"&gt;True</span>,j)
        j = (j + <span class="hljs-number"&gt;1</span>) % len(self._table)  <span class="hljs-comment" style="color: #738191;"&gt;# 向下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>查找</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_getitem</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    found,s = self._find_slot(j,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._table[s].value

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_setitem</span><span class="hljs-params"&gt;(self,v)</span>:</span>
    found,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:  <span class="hljs-comment" style="color: #738191;"&gt;# 使用第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
        self._table[s] = self.Item(k,v)
        self._n += <span class="hljs-number"&gt;1</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;else</span>:
        self._table[s].value = v

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_delitem</span><span class="hljs-params"&gt;(self,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    self._table[s] = HashMapTwo._AVAIL  <span class="hljs-comment" style="color: #738191;"&gt;# <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> j <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> range(len(self._table)):
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> self._is_available(j):
            <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> self._table[j].key

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_is_available</span><span class="hljs-params"&gt;(self,j)</span>:</span>
    <span class="hljs-string" style="color: #0048ab;"&gt;"""判断该位置是否可用"""</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;or</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> HashMapTwo._AVAIL

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_find_slot</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    <span class="hljs-string" style="color: #0048ab;"&gt;"""寻找键k所在的索引
       如果找到了,返回(True,索引)
       如果没找到,返回(False,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的索引位置)"""</span>

    firstAvail = <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;while</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;True</span>:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._is_available(j):
            <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> firstAvail <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:  <span class="hljs-comment" style="color: #738191;"&gt;# _AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>可以是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
                firstAvail = j
            <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;"&gt;is</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;None</span>:  <span class="hljs-comment" style="color: #738191;"&gt;# 跳过_AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
                <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (<span class="hljs-keyword" style="font-weight: bold;"&gt;False</span>,firstAvail)
        <span class="hljs-keyword" style="font-weight: bold;"&gt;elif</span> k == self._table[j].key:
            <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> (<span class="hljs-keyword" style="font-weight: bold;"&gt;True</span>,j)
        j = (j + <span class="hljs-number"&gt;1</span>) % len(self._table)  <span class="hljs-comment" style="color: #738191;"&gt;# 向下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>查找</span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_getitem</span><span class="hljs-params"&gt;(self,k)</span>:</span>
    found,s = self._find_slot(j,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    <span class="hljs-keyword" style="font-weight: bold;"&gt;return</span> self._table[s].value

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_setitem</span><span class="hljs-params"&gt;(self,v)</span>:</span>
    found,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:  <span class="hljs-comment" style="color: #738191;"&gt;# 使用第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
        self._table[s] = self.Item(k,v)
        self._n += <span class="hljs-number"&gt;1</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;else</span>:
        self._table[s].value = v

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;_inner_delitem</span><span class="hljs-params"&gt;(self,k)
    <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> found:
        <span class="hljs-keyword" style="font-weight: bold;"&gt;raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;"&gt;'Key Error: '</span> + repr(k))
    self._table[s] = HashMapTwo._AVAIL  <span class="hljs-comment" style="color: #738191;"&gt;# <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>

<span class="hljs-function"&gt;<span class="hljs-keyword" style="font-weight: bold;"&gt;def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"&gt;__iter__</span><span class="hljs-params"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="font-weight: bold;"&gt;for</span> j <span class="hljs-keyword" style="font-weight: bold;"&gt;in</span> range(len(self._table)):
        <span class="hljs-keyword" style="font-weight: bold;"&gt;if</span> <span class="hljs-keyword" style="font-weight: bold;"&gt;not</span> self._is_available(j):
            <span class="hljs-keyword" style="font-weight: bold;"&gt;yield</span> self._table[j].key


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶