新聞中心
這里有您想知道的互聯(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]語句從映射中刪除鍵-值對(duì)。
len()回到映射中存儲(chǔ)的鍵-值對(duì)的數(shù)目。
當(dāng)鍵存在時(shí),in通過keyinmap等語句返回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://fisionsoft.com.cn/article/djdiigp.html


咨詢
建站咨詢
