#!/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的更多相关文章

  1. “笨方法”学习Python笔记(1)-Windows下的准备

    Python入门书籍 来自于开源中国微信公众号推荐的一篇文章 全民Python时代,豆瓣高级工程师告诉你 Python 怎么学 问:请问你目前最好的入门书是那本?有没有和PHP或者其他语言对比讲Pyt ...

  2. LPTHW 笨方法学习python 16章

    根据16章的内容作了一些扩展. 比如,判断文件如果存在,就在文件后追加,如不存在则创建. 同时借鉴了shell命令中类似 cat <<EOF > test的方法,提示用户输入一个结尾 ...

  3. “笨方法”学习Python笔记(2)-VS Code作为文本编辑器以及配置Python调试环境

    Visual Studio Code 免费跨平台文本编辑器,插件资源丰富,我把其作为Debug的首选. 下载地址:https://code.visualstudio.com/Download 安装之后 ...

  4. 笨办法学习python-ex51自我理解笔记

    本章节主要讲的是web的工作原理,先大概熟悉记录一下,为以后写Django web框架打下基础. web工作原理: 1.用户从浏览器输入网址----->browser通过电脑中的网络设备(网卡) ...

  5. 笨办法学习python-ex41源码加自己注释

    #!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi import random from urllib import urlop ...

  6. 笨办法学习python3练习代码:argv参数变量与文件操作

    ex15.py 完成ex15.py需要在ex15.py同文件夹目录下面准备一个txt文件(ex15_sample.txt) 执行ex15.py 如: python     ex15.py      e ...

  7. 笨办法学习Python3练习代码1-10

    ex1.py print("hello world!",end = " ")#不换行 print("hello again") print( ...

  8. 笨办法学习python3练习代码ex20.py 函数和文件

    注意,还要在python3,就是ex20.py的同目录里面直接创建一个ex20.txt的文件.里面至少要有三行内容 #函数和文件 #readline:只读取文本文件的一行 #seek(0):将读写位置 ...

  9. 学习Python第一天:找了4本专属小白的书籍(前期入门打基础)

    我们提供一个初学者最好的Python书籍列表.Python是一个初级程序员可以学习编程的最友好语言之一.为了帮助您开始使用Python编程,我们分享此列表.泡一杯茶,选一本书阅读,开始使用Python ...

随机推荐

  1. JVM 真的很难学么?不、只是你“不敢学”而已

    JVM 真的很难学么?不.只是你"不敢学"而已        许多招聘的信息上面都说,要了解jvm.多线程什么的对于 java 程序员来说,这是工作好多年的程序员都不一定能掌握的东 ...

  2. linux 批量删除文件

    来源;https://www.cnblogs.com/sinpo/p/7106998.html linux下批量删除文件   1. 在linux批量删除多级目录下同一格式的文件,可采用find + e ...

  3. diskpart 分区,挂载,和移除

    list disk select disk 1 clean Create partition primary size=102400 active format quick list volume a ...

  4. Spring5参考指南:Bean的生命周期管理

    文章目录 Spring Bean 的生命周期回调 总结生命周期机制 startup和Shutdown回调 优雅的关闭Spring IoC容器 Spring Bean 的生命周期回调 Spring中的B ...

  5. Apache Commons Lang &#187; 3.10使用简介

    ============================================================= 行文介绍: 1.诞生背景 2.引入方案 3.简单介绍 4 .详情介绍 文档: ...

  6. 学数据库你竟然不用用JAVA写代码,可惜你遇到了我! JAVA连接数据库(JDBC)的安装使用教程

    Step 1 你得有Eclipse 没有出门右拐,我教不了你. Step 2 你得有Mysql MySQL的详细安装过程,我在另一篇博客中给出.戳我 Step 3 安装JDBC 可以去官网下,如果用的 ...

  7. C++条件分支结构

    一.对于近期学习知识点的摘要: 1. 从第一个.cpp文件谈起, #include<iostream> //头文件 using namespace std; //使用命名空间,namesp ...

  8. prufer编码学习笔记

    prufer 编码 对于一个无根树,他的 prufer 编码是这样确定的: 每次找到编号最小的一个叶子节点,也就是度数为\(1\)的节点,把和它相连的点,加入 prufer 编码序列的末尾,然后把这个 ...

  9. Java大数据秋招面试题

    以下为整理的自己秋招遇到的面试题:主要是Java和大数据相关题型:根据印象整理了下,有些记不起来了. 死锁.乐观锁.悲观锁synchronized底层原理及膨胀机制ReetrantLock底层原理,源 ...

  10. muduo网络库源码学习————Timestamp.cc

    今天开始学习陈硕先生的muduo网络库,moduo网络库得到很多好评,陈硕先生自己也说核心代码不超过5000行,所以我觉得有必要拿过来好好学习下,学习的时候在源码上面添加一些自己的注释,方便日后理解, ...