笨办法学习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 ...
随机推荐
- nignx location index的用法
来源:https://blog.csdn.net/qq_32331073/article/details/81945134#_10 index指令的作用 在前后端分离的基础上,通过Nginx配置,指定 ...
- 防止html标签转义
function htmlDecode ( str ) { var ele = document.createElement('span'); ele.innerHTML = str; return ...
- 前端日期时间处理建议使用Momen
使用方法 下载: http://momentjs.cn/downloads/moment.js 多语言版本:http://momentjs.cn/downloads/moment-with-local ...
- PHP中级篇 Apache配置httpd-vhosts虚拟主机总结及注意事项
经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主机时都习惯性的ctrl+c和ctrl+v,这次由于重装系统,需要配置一个新的PHP开发环境虚拟主机,于是总结一下Apaceh配置http ...
- 关于搭建IIS网页弹出登录框的解决方案
今天自己搭建IIS服务器的时候,明明设置了匿名访问,但是用ie访问127.0.0.1的时候还是会弹出一个登陆框,最后在网上找到答案. 转自: https://blog.csdn.net/sunleib ...
- Solidity的Bytecode和Opcode简介
Solidity的Bytecode和Opcode简介 随着我们更深入地编写智能合约,我们将遇到诸如" PUSH1"," SSTORE"," CALLV ...
- SQL计算算数表达式的函数自定义(加减乘除)
一.整体思路:循环遍历表达式字符串,设置一个index从第一个字符开始检测当前数字是否可以和后面的数字进行运算,如果可以运算,将两个数挑出来运算,然后用运算的结果替换原来表达式中的这两个数和符号,计算 ...
- MySQL简介和安装
一.关系型数据库初识 1.1 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我 ...
- SSH 超时设置
在阿里云买了一台乞丐版服务器,搭了一个博客,安装了java,mysql,redis等服务,把以前写的知乎爬虫部署上去,看看爬取效果.程序运行一段时间后,发现cmder上的日志不打了,我原以为爬虫挂了, ...
- Android多线程下载远程图片
修改后的代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...