Dictionaries and lists
Lists can appear as values in a dictionary. For example, if you were given a dictionary that maps from letters to frequencies, you might want to invert it; that is, create a dictionary that maps from frequencies to letters. Since there might be several letters with the same frequency, each value in the inverted dictionary should be a list of letters.
Each time through the loop, key gets a key from d and val gets the corresponding value. If val is not in dic, that means we haven’t seen it before, so we create a new item and initialize it with a singleton (a list that contains a single element). Otherwise we have seen this value before, so we append the corresponding key to the list.
Lists can be values in a dictionary, but they cannot be keys.
A dictionary is implemented using a hashtable and that means that the keys have to be hashable.
A hash is a function that takes a value (of any kind) and returns an integer. Dictionaries use these integers, called hash values, to store and look up key-value pairs. This system works fine if the keys are immutable. But if the keyws are mutable, like lists, bad things happen. For example, when you create a key-value pair, Python hashed the key and stores it in the corresponding location. If you modify the key and then hash it again, it would go to a different location. In that case you might have two entries for the same key, or you might not be able to find a key. Either way, the dictionary wouldn’t work correctly. That’s why the keys have to be hashable, and why mutable types like lists aren’t. the simplest way to get around this location is to use tuples. Since dictionaries are mutable, they can’t be used as keys, but they can be used as values.
setdefault(key[, default])
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
from Thinking in Python
Dictionaries and lists的更多相关文章
- Think Python - Chapter 11 - Dictionaries
Dictionaries A dictionary is like a list, but more general. In a list, the indices have to be intege ...
- Dictionaries
A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...
- Python学习笔记(迭代、模块扩展、GUI 、编码处理等)
PythonIDLE中的编码处理 http://www.tuicool.com/articles/NbyEBr 原文标题:Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来 ...
- 关于Python中数据对象的可变性
先贴上Python官网中对数据模型描述的几段话.(在python官网的 语言参考>>数据模型 那部分) Every object has an identity, a type and a ...
- (转)The Road to TensorFlow
Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a ...
- 在tornado中使用celery实现异步任务处理之中的一个
一.简单介绍 tornado-celery是用于Tornado web框架的非堵塞 celeryclient. 通过tornado-celery能够将耗时任务增加到任务队列中处理, 在celery中创 ...
- Understanding ROS Services and Parameters
service是nodes之间通信的一种方式,允许nodes send a request and recieve a response. rosservice rosparam roservice ...
- python之多线程 threading.Lock() 和 threading.RLock()
0.目录 2. threading.Lock() 的必要性3.观察block4.threading.RLock() 的应用场景 1.参考 Thread Synchronization Mechanis ...
- ROS学习手记 - 5 理解ROS中的基本概念_Services and Parameters
上一节完成了对nodes, Topic的理解,再深入一步: Services and Parameters 我不理解为何 ROS wiki 要把service与parameter放在一起介绍, 很想分 ...
随机推荐
- [Gatsby] Install Gatsby and Scaffold a Blog
In this lesson, you’ll install Gatsby and the plugins that give the default starter the ability to t ...
- MyEclipse2014高速配置Spring & Spring Testing, Spring AOP简单使用
1.新建项目 2.右击项目,如图,利用myeclipse自己主动导入spring 3.在弹出的对话框中一直next到最后,在最后的页面中勾选Spring Testing,完毕. watermark/2 ...
- Android px,dp,pt,sp的差别
px(像素点) mm 等Android不建议用 为什么电脑web开发能够用而Android不建议用? 由于px代表像素点个数,一般电脑分辨率都同样 不管14寸还是15寸都是1366*768而手机分辨率 ...
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 【linux】——centos 分辨率配置
用过centos的朋友肯定知道centos在默认安装的时候显示器的分辨率只有800*600,但是我们想把改成1024*768或者更大,怎么办呢,我也是试过了才知道,首先打开系统-管理-显示-硬件-显示 ...
- [luogu P1776] 宝物筛选 解题报告(单调队列优化DP)
题目链接: https://www.luogu.org/problemnew/show/P1776 题目: 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF ...
- Linux系统安装Redis数据库
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- JQuery中的find、filter和each方法学习
find() 概述 搜索所有与指定表达式匹配的元素.这个函数是找出正在处理的元素的后代元素的好方法. 所有搜索都依靠jQuery表达式来完成.这个表达式可以使用CSS1-3的选择器语法来写. 参数 e ...
- sql中对日期的筛选
#几个小时内的数据 DATE_SUB(NOW(), INTERVAL 5 HOUR) #今天 select * from 表名 where to_days(时间字段名) = to_days(now() ...
- eclipse界面更改为黑色
效果如下: 更改很简单,该两个配置就行了,如下图: 1.在window中打开Preferences,然后跟下图一样配置就行了.