#!/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. pytorch 去除维度为1的维度

    out.squeeze(dim=1) out.squeeze_(dim=1)

  2. 杂园日记-获取URL参数

    function getUrlParams(name, url){ var locationUrl = window.location.search; if(url){ var s =url.inde ...

  3. Ubuntu 18.04更换apt-get源

    使用apt-get安装时,会很慢,更换了国内的源后,就可以解决这个问题了. 1. 备份sources.list文件 sudo cp /etc/apt/sources.list /etc/apt/sou ...

  4. CodeForces 674C Levels and Regions

    #include<bits/stdc++.h> using namespace std; const int maxn=2e5+5; int N,K,head,tair; int q[ma ...

  5. 浅谈 Objective-C Associated Objects

    简介 Associated Objects 是 Objective-C 2.0 中 Runtime 的特性之一. 在 <objc/runtime.h> 中定义的三个方法, void obj ...

  6. 打造livecd的注意事项

    一:在CentOS.ks的定制脚本中,删除syslinux组件:出错提示: /usr/lib/python2.6/site-packages/imgcreate/errors.py:45: Depre ...

  7. CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)

    ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from ...

  8. C++基本知识总结

    从第一个CPP开始写起: "hello,world" #include<iostream> using namespace std;//使用所有命名空间 int mai ...

  9. commons-logging slf4j log4j 区别

    日志门面 1.Apache通用日志接口(commons-logging.jar) Apache Commons包中的一个,包含了日志功能,必须使用的jar包.这个包本身包含了一个Simple Logg ...

  10. 揭露.net培训结构软谋收钱踢学员的套路

    本人以下文章全部真实,希望管理员能通过,给更多的.net学者一个警示,避免更多的.neter掉入泥坑. 本人小码农一枚,主要做.net方向,苦于进步无门,各种资料收集渠道受限,最后狠心花一个月工资报名 ...