python实现哈希表

代码如下:

#! /usr/bin/env python#coding=utf-8#实现哈希表(线性地址再散列)

def changekey(key,m,di): key01=(key+di) % m return key01

a=raw_input(“please entry the numbers:\n”).split()m=len(a)dict01={}for i in a: key=int(i)%m if “%s”%key in dict01: newkey=changekey(key,m,1) while “%s”%newkey in dict01: #因为下面的dict01的key值是以字符串来保存,因此这里作判断时也要用字符串格式 newkey=changekey(newkey,m,1) dict01[“%s”%newkey]=int(i) else: dict01[“%s”%key]=int(i)print dict01