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 dictionary:

Combining this feature with zip yields a concise way to create a dictionary:

The dictionary method update also takes a list of tuples and adds them, as key-value pairs, to an existing dictionary.

Combining items, tuples assignment and for, you get the idiom for traversing the keys and values of a dictionary:

It is common to use tuples as keys in dictionaries (primary because you can’t use lists). For examples, a telephone directory might map from last-name, first-name pairs to telephone numbers. Assuming that we have defined last, first and number, we could write:

directory[last,first] = number

The expression in brackets is a tuple. We could use tuple assignment to traverse this dictionary.

for last, first in directory:
print first, last, directory[last,first]

This loop traverse the keys in directory, which are tuples. It assigns the elements of each tuple to last and first, then prints the name and corresponding telephone number. There are two ways to represent tuples in a state diagram. The more detailed version shows the indices and elements just as they appear in a list. For example, the tuple (‘Cleese’, ‘John’) would appear:

But in a larger diagram you might want to leave out the details. For example, a diagram of the telephone directory might appear:

Here the tuples are shown using Python syntax as a graphical shorthand.

from Thinking in Python

Dictionaries and tuples的更多相关文章

  1. Think Python - Chapter 12 Tuples

    12.1 Tuples are immutable(元组是不可变的)A tuple is a sequence of values. The values can be any type, and t ...

  2. Effective Python2 读书笔记3

    Item 22: Prefer Helper Classes Over Bookkeeping with Dictionaries and Tuples For example, say you wa ...

  3. python 内建类型

    ''' 数值 numbers 字符串 strings 列表 lists 字典 dictionaries 元组 tuples 文件 files 集合 sets ''' 1.1 序列的操作 所有序列类型都 ...

  4. Think Python - Chapter 17 - Classes and methods

    17.1 Object-oriented featuresPython is an object-oriented programming language, which means that it ...

  5. Object-oriented features

    Python is an object-oriented programing language, which means that it provides features that support ...

  6. Python 核心数据类型

    1.Python中一切皆对象 2.Python中不需要申明对象类型,对象的类型由运行的表达式决定 3.创建了对象意味着绑定了对象的操作到此对象,也就是在固有的对象上只能调用该对象特有的操作.比如只能将 ...

  7. ODI KM二次开发手册

    ODI KM二次开发手册   分类: ODI(16) 目录(?)[+] 1 引言 1.1 编写目的 本手册面向的读者对象为具备数据集成业务知识及对ODI操作了解的开发人员,作为其完成基于ODI基础上K ...

  8. <Using parquet with impala>

    Operations upon Impala Create table stored as parquet like parquet '/user/etl/datafile1' stored as p ...

  9. [转]python与numpy基础

    来源于:https://github.com/HanXiaoyang/python-and-numpy-tutorial/blob/master/python-numpy-tutorial.ipynb ...

随机推荐

  1. String转换成int型

    private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...

  2. CCS+C6678LE开发记录12:UIA组件的安装

    在安装了CCS 6.0版本的IDE和最新版的MCSDK后似乎一切都很完美,但事实并非如此. 当我试图编译SDK附带的image_processing (IPC based) demo时出现如下错误: ...

  3. zookeeper的节点类型

    Znode有两种类型: 短暂(ephemeral):客户端和服务器端断开连接后,创建的节点自己删除 持久(persistent):客户端和服务器端断开连接后,创建的节点不删除 2)Znode有四种形式 ...

  4. Android APP 调试过程中遇到的问题。

    调试过过程中APP安装完启动后有的时候会异常退出,报这个错误.有的时候可以直接启动.查找不到原因.网上说把commit方法替换成commitAllowingStateLoss() 也无效. Andro ...

  5. SQLServer 错误: 15404,维护计划无法执行

    错误症状: D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG下面的ERROELOG,用文本打,查看运行维维计划不成功是生成的错误日志详细信 ...

  6. HDU 3584 Cube 【 三维树状数组 】

    题意:还是那篇论文里面讲到的,三维树状数组http://wenku.baidu.com/view/1e51750abb68a98271fefaa8画个立方体出来对照一下好想一点 #include< ...

  7. POJ 3273 Monthly Expense 【二分答案】

    题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...

  8. python编程基础

    Date: 2019-05-27 Author: Sun 1. 程序 为了完成某种特定功能,以某种程序设计语言编写的有序指令的集合.程序是指挥cpu工作的"工作手册".计算机只能执 ...

  9. Calling Mojo from Blink

    Variants Let's assume we have a mojom file such as this:   module example.mojom;   interface Foo {   ...

  10. 线段树(segment tree )

    http://www.cnblogs.com/TenosDoIt/p/3453089.html 写的非常好! 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很 ...