日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python哈希散列的映射

1、散列的映射

Map()創(chuàng)建一個(gè)空映射,然后回到一個(gè)空映射集合。

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

get返回key對(duì)應(yīng)的值。如果key不存在,返回none。

del通過del map[key]語(yǔ)句從映射中刪除鍵-值對(duì)。

len()回到映射中存儲(chǔ)的鍵-值對(duì)的數(shù)目。

當(dāng)鍵存在時(shí),in通過keyinmap等語(yǔ)句返回True,否則返回False。

2、實(shí)例

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哈希散列的映射,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程


網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:python哈希散列的映射
文章起源:http://www.5511xx.com/article/djdiigp.html