笨办法学习python之hashmap
#!/user/bin/env python
#-*-coding:utf-8 -*-
#Author: qinjiaxi
#初始化aMap列表,把列表num_buckets添加到aMap中,num_bukets用来存hashmap里设置的内容
def new(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 hash_key(aMap, key):
"""Given a key this will create a number and then convert it to an index for the aMap's buckets."""
return hash(key) % len(aMap)#利用求余(模除)来获得一个放置key的位置,其中hash()函数是用来获取一个数字或者字符串的哈希值(是一个整数)。
def get_bucket(aMap, key):
"""Given a key , find the bucket where it would go ."""
bucket_id = hash_key(aMap, key)
return aMap[bucket_id]#通过bucket_id获取bucket
#使用enumerate()和for循环遍历bucket,用key获取索引、键、值
def get_slot(aMap, key, default = None):
"""
Returns the index, key, and value of a slot found in a slot found in a bucket.
Returns -1, key, and default(None if not set) when not found.
"""
bucket = get_bucket(aMap, key)
for i, kv in enumerate(bucket):
k, v = kv
if key == k:
return i, k ,v
return -1, key, default
#取值
def get(aMap, key ,default = None):
"""Gets the value in a bucket for the given key, or the default."""
i, k, v = get_slot(aMap, key, default = None)
return v
#设置键值对追加到字典中,保证每个key存储一次
def set(aMap, key, value):
"""Set the key to the value, replacing any existing value."""
bucket = get_bucket(aMap, key)
i, k, v = get_slot(aMap, key)
#如果位置存在就代替
if i >= 0:
#the key exists, replace it
bucket[i] = (key, value)
#不存在就追加到字典中
else:
#the key does not ,append to creat it
bucket.append((key, value))
#通过key删除bucket
def delete(aMap, key):
""" Deletes the given key from the Map."""
bucket = get_bucket(aMap, key)
for i in range(len(bucket)):
k, v = bucket[i]
if key == k:
del bucket[i]
break
#调试打印hashmap功能
def list(aMap):
"""Prints out what's in the Map"""
for bucket in aMap:
if bucket:
for k,v in bucket:
print (k, v)
笨办法学习python之hashmap的更多相关文章
- “笨方法”学习Python笔记(1)-Windows下的准备
Python入门书籍 来自于开源中国微信公众号推荐的一篇文章 全民Python时代,豆瓣高级工程师告诉你 Python 怎么学 问:请问你目前最好的入门书是那本?有没有和PHP或者其他语言对比讲Pyt ...
- LPTHW 笨方法学习python 16章
根据16章的内容作了一些扩展. 比如,判断文件如果存在,就在文件后追加,如不存在则创建. 同时借鉴了shell命令中类似 cat <<EOF > test的方法,提示用户输入一个结尾 ...
- “笨方法”学习Python笔记(2)-VS Code作为文本编辑器以及配置Python调试环境
Visual Studio Code 免费跨平台文本编辑器,插件资源丰富,我把其作为Debug的首选. 下载地址:https://code.visualstudio.com/Download 安装之后 ...
- 笨办法学习python-ex51自我理解笔记
本章节主要讲的是web的工作原理,先大概熟悉记录一下,为以后写Django web框架打下基础. web工作原理: 1.用户从浏览器输入网址----->browser通过电脑中的网络设备(网卡) ...
- 笨办法学习python-ex41源码加自己注释
#!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi import random from urllib import urlop ...
- 笨办法学习python3练习代码:argv参数变量与文件操作
ex15.py 完成ex15.py需要在ex15.py同文件夹目录下面准备一个txt文件(ex15_sample.txt) 执行ex15.py 如: python ex15.py e ...
- 笨办法学习Python3练习代码1-10
ex1.py print("hello world!",end = " ")#不换行 print("hello again") print( ...
- 笨办法学习python3练习代码ex20.py 函数和文件
注意,还要在python3,就是ex20.py的同目录里面直接创建一个ex20.txt的文件.里面至少要有三行内容 #函数和文件 #readline:只读取文本文件的一行 #seek(0):将读写位置 ...
- 学习Python第一天:找了4本专属小白的书籍(前期入门打基础)
我们提供一个初学者最好的Python书籍列表.Python是一个初级程序员可以学习编程的最友好语言之一.为了帮助您开始使用Python编程,我们分享此列表.泡一杯茶,选一本书阅读,开始使用Python ...
随机推荐
- jdbc-手写Java方法连接数据库
一.关键四元素 ① com.mysql.jdbc.Driver mysql数据库连接jar包. 获取途径: 链接:https://pan.baidu.com/s/1SFcjuu ...
- 大部分人都不知道的8个python神操作
01 print 打印带有颜色的信息 大家知道 Python 中的信息打印函数 Print,一般我们会使用它打印一些东西,作为一个简单调试. 但是你知道么,这个 Print 打印出来的字体颜色是可以设 ...
- python学习18类4之静态类
'''''''''类的静态方法.普通方法.类方法 静态方法: 用 @staticmethod 装饰的不带 self 参数的方法叫做静态方法,类的静态方法可以没有参数,可以直接使用类名调用. 普通方法: ...
- (c++ std) 查找 vector 中的元素
You can use std::find from <algorithm>: std::find(vector.begin(), vector.end(), item) != vecto ...
- Zookeeper之Error contacting service. It is probably not running.
安装ZooKeeper时,无论是修改zoo.cfg:还是myid,都检查了几遍都没有错误.但是开启Zookeeper服务时出现: Error contacting service. It is pro ...
- Waiting for another flutter command to release the startup lock...
2019独角兽企业重金招聘Python工程师标准>>> rm ./flutter/bin/cache/lockfile info from 转载于:https://my.oschin ...
- Java的循环语句
一.while 循环 while(循环条件){ 循环操作语句 } * 循环3要素: 变量的初值.变量的判断.变量的更新 * 缺少循环变量的更新,循环将一直进行下去 public class Whlie ...
- jQuery里面click、this事件遇到(Django模型里for)相同的id名和class名想获取值
遇到的原型是这样的!下面我把它简化一下; click事件: 在浏览器里面只能获取横线上面的值,和下面的第一个值!! 这是因为id等级比class高,而且js要求id不能重复! 当 转载于:https: ...
- 线程状态及各状态下与锁和CPU的关系
线程的状态 Thread.State枚举类型中定义了线程的六种状态:NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING和TERMINATED. 线程在某一时刻只能拥有 ...
- Unity碰撞检测
2019独角兽企业重金招聘Python工程师标准>>> 我们在用unity做开发的时候,会遇到要用到碰撞检测的问题,比如说,物体撞到墙壁,子弹打到物体等等,所以这里简单介绍一下uni ...