#!/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. 算法笔记刷题3(codeup 5901)

    今天刷题的速度依旧很慢(小乌龟挥爪.jpg) 我觉得codeup5901中回文串的处理很妙,如果是我自己写的话可能会把数组直接倒过来和原来对比.按照对称规律进行比对的话,工作量可以减少一半. #inc ...

  2. Android 中 OkHttp 三步实现生命周期绑定

    简介 OkHttps 是 OkHttp 增强版的超轻量封装包. 和 Retrofit 相比,它更加轻量(只有 59Kb),是 Retrofit (124Kb)的一半,而且更加的开箱即用,API 更加自 ...

  3. python学习笔记(五)---函数与类

    函数 def为定义函数的一个标志 demo1: def greet_user(username): print("Hello, " + username.title() + &qu ...

  4. 微信网页授权报code been used, hints: [ req_id: XYv1Ha07042046 ]

    先贴上代码: public function index() { $code = input('get.code'); $tool = new Wxtool(); if (empty($code)) ...

  5. linux--配置开发环境 --Nginx篇

    安装: 安装好了话,我们的nginx的目录在:  /etc/nginx 启动: sudo service nginx start 然后访问我们的页面就可以看到了我们的界面 然后我们配置我们的域名: 我 ...

  6. python学习19类5之多态与鸭子模型

    '''''''''一.多态1.Python中多态是指一类事物有多种形态.''' class Animal: def run(self): raise AttributeError('子类必须实现这个方 ...

  7. NumPy学习指南(第2版)

    第一章 NumPy快速入门 首先,我们将介绍如何在不同的操作系统中安装NumPy和相关软件,并给出使用NumPy的简单示例代码. 然后,我们将简单介绍IPython(一种交互式shell工具). 如前 ...

  8. mac OS 安装淘宝npm镜像

    淘宝npm镜像官网 https://npm.taobao.org/ 在终端输入 npm install -g cnpm --registry=https://registry.npm.taobao.o ...

  9. RF(页面断言)

    一.RF中断言方式 title should be(断言title与预期指定的title内容相等) Open Browser https://www.baidu.com/ gc Title Shoul ...

  10. 重新认识Java注解

    重新认识Java注解 今天Debug看源码的时候,无意间看到这么个东西 首先承认我的无知,看到这个我很惊诧. 也勾起了我的好奇心,于是有了这篇认知记录. 下面就来重新认识下注解吧! 注解的本质 关于运 ...