一入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:我就坐在旁边看他敲着代码,感觉很好奇.感觉很酷,从那之后,我就想和学长一样的厉害,就想让学长教我,请他吃了一周的饭,他答应了.从此 ...
随机推荐
- [Everyday Mathematics]20150104
设 $a>0$, $$\bex x_1=1,\quad x_{n+1}=x_n+an\prod_{i=1}^n x_i^{-\frac{1}{n}}. \eex$$ 试证: $$\bex \vl ...
- Mysql 多表联合查询效率分析及优化
1. 多表连接类型 1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用',' 如: SELECT * FROM table1 CROSS JO ...
- 转载:Hadoop权威指南学习笔记
转自:http://pieux.github.io/blog/2013-05-08-learn-hadoop-the-definitive-guide.html 1 前言 Hadoop的内部工作机制: ...
- SDUT 3571 Password 暴力搜索
这个题如果裸搜肯定超时了 但是我们可以枚举,用初始串的哪一位数字去填目标串的那一位数字 这样就是暴力6!,复杂度很低,然后需要解决过程中经过的点的问题, 因为是从左向右走,所以记录当前光标, 和当前达 ...
- 022 UFT虚拟对象
虚拟对象: 程序中那些行为标准类型对象的对象,但不能被QTP识别,则可把这些对象类型称为虚拟对象.并且映射到某类标准对象,例如button,check box等,QTP在测试过程中就会对这些虚拟对象模 ...
- node.js study: cluster
从v0.6.x开始,Node.js提供了多进程模块cluster,允许创建一组进程来共享同一个socket,并且分担负载压力.官方文档是这样说的:A single instance of Node.j ...
- void、void*以及NULL
void.void*以及NULL 写在前面 在使用C++的过程中,void和NULL用到的频率挺高的,但是从来没有去探索过这两个关键字的联系和区别,也没有对它们做更多的探索.对于void*,说实话,实 ...
- document.body、document.documentElement和window获取视窗大小的区别
来源:http://www.ido321.com/906.html 在w3school关于window对象的介绍中,介绍了获取浏览器窗口大小的三种方法(浏览器的视口,不包括工具栏和滚动条). 对于In ...
- Azure终于支持大容量虚拟机了-最高32核,448G内存
Azure终于支持大容量虚拟机了-最高32核,448G内存 最近微软Azure虚拟机旗下的大容量G系列虚拟机通用版本正式上线.G系列虚拟机方案提供公有云领域最大的内存容量.最强处理能力以及空间可观的本 ...
- vs2010 无法连接到asp.net development server
http://blog.csdn.net/xqf309/article/details/7881257 今天打开之前的程序,按了F5进行调试,等了会弹出窗体来说:无法连接到asp.net develo ...