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

  1. Think Python - Chapter 11 - Dictionaries

    Dictionaries A dictionary is like a list, but more general. In a list, the indices have to be intege ...

  2. Dictionaries

    A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...

  3. Python学习笔记(迭代、模块扩展、GUI 、编码处理等)

    PythonIDLE中的编码处理 http://www.tuicool.com/articles/NbyEBr 原文标题:Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来 ...

  4. 关于Python中数据对象的可变性

    先贴上Python官网中对数据模型描述的几段话.(在python官网的 语言参考>>数据模型 那部分) Every object has an identity, a type and a ...

  5. (转)The Road to TensorFlow

    Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a ...

  6. 在tornado中使用celery实现异步任务处理之中的一个

    一.简单介绍 tornado-celery是用于Tornado web框架的非堵塞 celeryclient. 通过tornado-celery能够将耗时任务增加到任务队列中处理, 在celery中创 ...

  7. Understanding ROS Services and Parameters

    service是nodes之间通信的一种方式,允许nodes send a request and recieve a response. rosservice rosparam roservice ...

  8. python之多线程 threading.Lock() 和 threading.RLock()

    0.目录 2. threading.Lock() 的必要性3.观察block4.threading.RLock() 的应用场景 1.参考 Thread Synchronization Mechanis ...

  9. ROS学习手记 - 5 理解ROS中的基本概念_Services and Parameters

    上一节完成了对nodes, Topic的理解,再深入一步: Services and Parameters 我不理解为何 ROS wiki 要把service与parameter放在一起介绍, 很想分 ...

随机推荐

  1. CURL库的宏定义列表

    列表CURL库一共同拥有17个函数 curl_close:关闭CURL会话 curl_copy_handle:复制一个CURL会话句柄,同一时候3复制其全部參数 curl_errno:返回最后一个错误 ...

  2. Linux - Redmine使用方式 | SVN提交代码

    Redmine使用方式 | SVN提交代码 本文地址:http://blog.csdn.net/caroline_wendy RbTools 1. 安装: svn co https://dev.cxx ...

  3. 面向程序猿的设计模式 ——GoF《设计模式》读书总结(壹)抽象工厂&生成器

    第一部分:创建型模式 创建型模式抽象了实例化过程. 它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.(把一些小的对象组装成大对象,这个工作由专门的类对象来做) 一个类创建型模式使用继承改变被实 ...

  4. C++_关于const 的全面总结

    C++中的constkeyword的使用方法很灵活.而使用const将大大改善程序的健壮性.本人依据各方面查到的资料进行总结例如以下,期望对朋友们有所帮助. Const 是C++中经常使用的类型修饰符 ...

  5. MyEclipse打包可运行的jar包

    详细步骤: Export... -> java -> Runnable JAR file Launch configuration:选择main方法所在的文件/类 Export desti ...

  6. 【HDOJ 5407】 CRB and Candies (大犇推导

    pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...

  7. 2016 ICPC CAMP Recording

    等了好久终于等到今天 马上能和群巨们一起学习了 希望不要暴露我太弱的本质............ 北京不冷,就是风大~~~ 1.24 8点准时起床了,准备下楼吃早饭 (这个宾馆好多美美的空姐对面就是东 ...

  8. 37、ifconfig命令

    很多windows很熟悉ipconfig命令行工具.它被用来获取网络接口配置信息并对此进行改动.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config). 通常须 ...

  9. 使用XMLHttpRequest解析json

    不适用内函数或者promise的方式,可以在外部提取到json数据 <!DOCTYPE html> <html lang="en"> <head> ...

  10. 一题多解(一) —— list(Python)判空(以及 is 与 == 的区别)

    >> l = [] 1. == >> l == [] True 2. not >> not l True 3. 注意 is 与 == 的区别 >> l ...