Looping and dictionaries

If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, print_hist prints each key and the corresponding value:

Print dictionary in alphabetical order:

Reverse lookup

Given a dictionary d and a key k, it is easy to find the corresponding value v = d[k]. This operation is called lookup. But what if you have v and you want to find k? You have two problems: first, there might be more than one key that maps to the value v. Depending on the application, you might be able to pick one, or you might have to make a list that contains all of them. Second, there is no simple syntax to do a reverse lookup; you have to search.

A reverse lookup is much slower than a forward lookup; if you have to do it often, or if the dictionary gets big, the performance of your program will suffer.

from Thinking in Python

Looping and dictionaries,布布扣,bubuko.com

时间: 2024-08-26 23:26:12

Looping and dictionaries的相关文章

Looping Techniques

When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method. >>> >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} >>> for k, v in knights.items(): ...

Python数据结构(二)

5.2. The del statement There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

Python 序列容器

Python中sequence主要包含存储单个元素序列和两个元素对的序列,str就是一个字符容器. 单元素序列主要有以下类型: bytearray: 字节数组,通过built-in 函数bytearray()创建 xrange:由函数xrange(n)/xrange(start, stop, step)创建,但不支持容器上的切片,连接,复制, in/not in 判断,min/max取值操作, xrange是 immutable sequence, xrange对象大小固定,与其代表范围大小无关

我要翻译《Think Python》-002 贡献列表 & 目录部分

PDF源文件地址 :  http://www.greenteapress.com/thinkpython/thinkpython.pdf 贡献列表 自从本书诞生之后,有超过上百个目光敏锐且有想法的读者给我发来了许多建议并指出了一些需要修正的地方.他们的热情和无私的奉献给了我巨大的帮助.如果你有任何建议或者发现需要修正的地方,请发邮件至:[email protected].如果您的建议被采纳,您的大名将会出现在我们的贡献人员列表(除非你本人过于低调拒绝承认).如果您发现文中的错误内容,敬请提供一下

Dictionaries and tuples

Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-value pair. As you should expect from a dictionary, the items are in no particular order. Conversely, you can use a list of tuples to initialize a new di

Python数据结构与算法--List和Dictionaries

Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还是牺牲了不常用的操作的性能来成全常用功能. 本文地址:http://www.cnblogs.com/archimedes/p/python-datastruct-algorithm-list-dictionary.html,转载请注明源地址. 设计者有很多的选择,使他们实现list的数据结构.这些选

一入python深似海--Dictionaries

定义及应用 定义 <span style="font-size:18px;">stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}#key:value pairs</span> 实例 # create a mapping of state to abbreviation states = { 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New Yo

Creating Dictionaries

Immutable dictionaries can be defined using the literal @{} syntax. But, like array literals, this was added relatively recently, so you should also be aware of the dictionaryWithObjectsAndKeys: and dictionaryWithObjects:forKeys: factory methods. All