<label id="bh7tp"><big id="bh7tp"></big></label>

<em id="bh7tp"><tr id="bh7tp"><u id="bh7tp"></u></tr></em>
<th id="bh7tp"></th>

<li id="bh7tp"><acronym id="bh7tp"></acronym></li>
  • <em id="bh7tp"></em>

    <tbody id="bh7tp"><pre id="bh7tp"></pre></tbody>
  • Python教程自學網歡迎您來學習!
    當前位置: 首頁 > Pyhon開發 > python哈希散列的映射

    python哈希散列的映射

       

    python哈希散列的映射

    python哈希散列的映射

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

    在put(key,val)的映射中添加新的鍵值對。若鍵已存在,則用新值代替舊值。

    get返回key對應的值。如果key不存在,返回none。

    del通過del map[key]語句從映射中刪除鍵-值對。

    len()回到映射中存儲的鍵-值對的數目。

    當鍵存在時,in通過keyinmap等語句返回True,否則返回False。

    python哈希散列的映射實例

    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
    人人爽人人澡人人人妻
    <label id="bh7tp"><big id="bh7tp"></big></label>

    <em id="bh7tp"><tr id="bh7tp"><u id="bh7tp"></u></tr></em>
    <th id="bh7tp"></th>

    <li id="bh7tp"><acronym id="bh7tp"></acronym></li>
  • <em id="bh7tp"></em>

    <tbody id="bh7tp"><pre id="bh7tp"></pre></tbody>