一入python深似海--dict(字典)的一种实现
以下是python中字典的一种实现。用list数据结构实现字典。详细是这种:[[(key1,value1),(key2,value2),...],[],[],...]
内部每个hash地址是一个list,存放hash地址同样的(key,value)对。
dict代码
def Map(num_buckets=256):
"""Initializes a Map with the given number of buckets."""
aMap = []
for i in range(0, num_buckets):
aMap.append([])
return aMap def Map_hash(aMap, key):
"""Given a key this will create a number and then convert it to
and index for the aMap's buckets."""
return hash(key) % len(aMap) def Map_get_bucket(aMap, key):
"""Given a key, find the bucket where it would go."""
bucket_id = Map_hash(aMap, key)
return aMap[bucket_id] def Map_get_slot(aMap, key, default=None):
"""Returns the index, key, and value of a slot found in a bucket."""
bucket = Map_get_bucket(aMap, key) for i, kv in enumerate(bucket):#bucket=[[k1,v1],[k2,v2],...]
k, v = kv
if key == k:
return i, k, v#ex1:i=0,k=k1,v=v1 return -1, key, default def Map_get(aMap, key, default=None):
"""Gets the value in a bucket for the given key, or the default."""
i, k, v = Map_get_slot(aMap, key, default=default)
return v def Map_set(aMap, key, value):
"""Sets the key to the value, replacing any existing value."""
bucket = Map_get_bucket(aMap, key)
i, k, v = Map_get_slot(aMap, key) if v:
bucket[i] = (key, value)#key/value pair
else:
bucket.append((key, value)) def Map_delete(aMap, key):
"""Deletes the given key from the Map."""
bucket = Map_get_bucket(aMap, key) for i in xrange(len(bucket)):
k, v = bucket[i]
if key == k:
del bucket[i]
break def Map_list(aMap):
"""Prints out what's in the Map."""
for bucket in aMap:
if bucket:
for k, v in bucket:
print k, v # The tests that it will work. jazz = Map()
Map_set(jazz, 'Miles Davis', 'Flamenco Sketches')
# confirms set will replace previous one
Map_set(jazz, 'Miles Davis', 'Kind Of Blue')
Map_set(jazz, 'Duke Ellington', 'Beginning To See The Light')
Map_set(jazz, 'Billy Strayhorn', 'Lush Life') print "---- List Test ----"
Map_list(jazz) print "---- Get Test ----"
print Map_get(jazz, 'Miles Davis')
print Map_get(jazz, 'Duke Ellington')
print Map_get(jazz, 'Billy Strayhorn') print "---- Delete Test ----"
print "** Goodbye Miles"
Map_delete(jazz, "Miles Davis")
Map_list(jazz) print "** Goodby Duke"
Map_delete(jazz, "Duke Ellington")
Map_list(jazz) print "** Goodbye Billy"
Map_delete(jazz, "Billy Strayhorn")
Map_list(jazz) print "** Goodbye Pork Pie Hat"
Map_delete(jazz, "Charles Mingus")
Map_hash()函数的解释例如以下:
a number. Python uses this function for its own dict data structure, and I'm just reusing it. You should fire up a Python console to see how it works. Once I have a number for the key, I then use the % (modulus)
operator and thelen(aMap) to get a bucket where this key can go. As you should know, the % (modulus)
operator will divide any number and give me the remainder. I can also use this as a way of limiting giant numbers to a fixed smaller set of other numbers. If you don't get this then use Python to explore it.
一入python深似海--dict(字典)的一种实现的更多相关文章
- 一入python深似海--浅拷贝与深拷贝
python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝. 要理解浅拷贝,必须先弄清楚python中的引用. 引用 Python中一切都是对象,变量中存放的是对象的引用 ...
- 一入python深似海--对象的属性
Python中一切皆是对象,每一个对象都能够有多个属性.Python是怎样管理这些属性呢?我们来探讨一下. 属性的__dict__系统 对象的属性包括两部分:类属性和对象属性.对象的属性可能来自于其类 ...
- 一入python深似海--class
python class 分为三个部分:class and object(类与对象),inheritance(继承),overload(重载)and override(覆写). class and o ...
- 一入python深似海--变量和对象
一.基本原理 Python中一切都是对象,变量是对象的引用. 这是一个普遍的法则.我们举个样例来说.Python是怎样来处理的. x = 'blue' y = 'green' z = x 当pytho ...
- 一入python深似海--range()、list与for
range使用方法 使用python的人都知道range()函数非常方便,今天再用到他的时候发现了非常多曾经看到过可是忘记的细节. 这里记录一下: range(1,5)#代表从1到5(不包括5) [1 ...
- 一入python深似海--python之道
python社区不乏幽默.先来看"python之道"这首诗. 导入this包: import this 输出是一首诗,这首诗总结了Python的风格,能够指导Python程序猿的编 ...
- 一入Python深似海--print
先给大家来个干货^~^,学习Python的一个好站点,http://learnpythonthehardway.org/book/ 经典样例 以下是几个老经典的样例喽,刚接触Python的能够敲一敲, ...
- 「一入 Java 深似海 」系列课程
第一期 「一入 Java 深似海 」系列课程 - 第一期 第一节:Java 语言基础
- 一入爬虫深似海,从此游戏是路人!总结我的python爬虫学习笔记!
前言 还记得是大学2年级的时候,偶然之间看到了学长在学习python:我就坐在旁边看他敲着代码,感觉很好奇.感觉很酷,从那之后,我就想和学长一样的厉害,就想让学长教我,请他吃了一周的饭,他答应了.从此 ...
随机推荐
- RMQ(dp)
我一开始是不知道有这么个东西,但是由于最近在学习后缀数组,碰到一道题需要用到后缀数组+RMQ解决的所以不得不学习了. 原理:用A[1...n]表示一组数,dp[i][j]表示从A[i]到A[i+2^j ...
- IOS-day03_OC中的get和set
OC中的get和set实质和C#/java中的一样 只是表现形式不同而已 如下: @interface Car : NSObject { int wheels; } -(void) run; -(vo ...
- 通过gdb调试分析Linux内核的启动过程
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验流程 1.打开环境 执 ...
- 机器学习----分布问题(二元,多元变量分布,Beta,Dir)
这涉及到数学的概率问题. 二元变量分布: 伯努利分布,就是0-1分布(比如一次抛硬币,正面朝上概率) 那么一次抛硬币的概率分布如下: 假设训练数据如下: 那么根据最大似然估计(MLE ...
- Codevs No.1281 Xn数列
2016-06-01 16:28:25 题目链接: Xn数列 (Codevs No.1281) 题目大意: 给定一种递推式为 Xn=(A*Xn-1+C)%M 的数列,求特定的某一项%G 解法: 矩阵乘 ...
- 【转】Nginx系列(二)--模块化
原博文出于: http://blog.csdn.net/liutengteng130/article/details/46700977 感谢! 高度模块化的设计设Nginx架构的基础.在Nginx中 ...
- App Extension编程指南(iOS8/OS X v10.10)中文版
http://www.cocoachina.com/ios/20141023/10027.html 当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思 ...
- Oracle的回收站和闪回查询机制(一)
实际工作中,我们经常会遇到一些情况,误删除某些表或某些表的某些记录,这时候就需要我们将这些记录重新插入进去.如何才能解决这个问题呢? Oracle的Flashback query(闪回查询)为我们解决 ...
- 玩转轻巧型C/C++ IDE之C-Free(配置GCC、Visual C++、Borland C++编译器)
玩转轻巧型C/C++ IDE之C-Free(配置GCC.Visual C++.Borland C++编译器) 之前在写一点简单的C/C++代码时习惯了VC++6.0,但是由于在windows7下VC6 ...
- 48种CIFilter
48种CIFilter CIAdditionCompositing //影像合成 CIAffineTransform //仿射变换 CICheckerboardGe ...