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

  1. Looping Techniques

    [Looping Techniques] 1.When looping through dictionaries, the key and corresponding value can be ret ...

  2. Think Python - Chapter 11 - Dictionaries

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

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

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

  4. python arguments *args and **args ** is for dictionaries, * is for lists or tuples.

    below is a good answer for this question , so I copy on here for some people need it By the way, the ...

  5. Arrays, Hashtables and Dictionaries

    Original article Built-in arrays Javascript Arrays(Javascript only) ArrayLists Hashtables Generic Li ...

  6. 【JS】Beginner3 & 4 & 5 & 6:Maths & Logic & Conditonal & Looping

    1.number operator () * / + - 2.logic make decisions in code compare values to produce a boolean valu ...

  7. IOS学习之路十九(JSON与Arrays 或者 Dictionaries相互转换)

    今天写了个json与Arrays 或者 Dictionaries相互转换的例子很简单: 通过 NSJSONSerialization 这个类的 dataWithJSONObject: options: ...

  8. 出现“java.lang.AssertionError: SAM dictionaries are not the same”报错

    运行一下程序时出现“java.lang.AssertionError: SAM dictionaries are not the same”报错 java -jar picard.jar SortVc ...

  9. [Python] 03 - Lists, Dictionaries, Tuples, Set

    Lists 列表 一.基础知识 定义 >>> sList = list("hello") >>> sList ['h', 'e', 'l', ' ...

随机推荐

  1. HDU 4372

    想了很久,终于想到了.... 向后看到F,向前看到B,假如把N-1个楼分成F+B个组,则把每个组最高的楼作为看到的楼,那么,其实在确定每一组的最高楼时,左边或右边的最高楼的顺序已经确定了.由于是排列数 ...

  2. group_concat函数

  3. ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】

    题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题 ...

  4. JavaSE入门学习24:Java面向对象补充

    一Java中的Object类 Object类是全部Java类的父类.假设一个类没有使用extendskeyword明白标识继承另外一个类,那么这个类默认 继承Object类. public class ...

  5. bzoj2768: [JLOI2010]冠军调查(双倍经验最小割)

    2768: [JLOI2010]冠军调查 题目:传送门 题解: 双倍经验(1934) 代码: #include<cstdio> #include<cstring> #inclu ...

  6. artTemplate的使用

    引言 腾讯的前端模板引擎,不逊于doT.js 1.引入 <script type="text/javascript" src="../plugin/artTempl ...

  7. Windows版Redis如何使用?(单机)

    使用Windows版Redis 1.下载Windows版本的Redis 2.在redis目录里创建redis.conf ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  8. Codeforces 982 C. Cut 'em all!(dfs)

    解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...

  9. 【原创】websphere部署war包报错

    应用程序在Tomcat上运行一切正常,但在websphere上部署时报以下错误:错误 500 处理请求时发生一个错误: /admin/upload.do 消息: WEB-INF/web.xml 详细错 ...

  10. HDU 1087 Super Jumping! Jumping! Jumping!【DP】

    解题思路:题目的大意是给出一列数,求这列数里面最长递增数列的和 dp[i]表示到达地点i的最大值,那么是如何达到i的呢,则我们可以考虑没有限制条件时候的跳跃,即可以从第1,2,3,---,i-1个地点 ...