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. PC端 java 开发蓝牙所遇到的问题

    由于项目的原因.要在电脑上开发一个通过蓝牙传送数据的client.我採用的是JAVA,JSME开发. client:去搜素蓝牙信号,然后找到对应的蓝牙信号进行连接. 服务端:client须要进行连接的 ...

  2. Webserver管理系列:4、WinPE

    WinPE能够识别NTFS分区,使用WinPE能够备份/还原系统.能够重置用户password. 首先给大家看下怎样用WinPE重置password: 放入WinPE光盘-〉启动后-〉点開始菜单-〉程 ...

  3. Android-HttpURLConnection自己主动管理cookie

    Volley那么好用的框架居然没有内置对cookie的处理,自己搞一个! public class MobCookieManager {//转载请标明出处:http://blog.csdn.net/g ...

  4. python 提取主域名和子域名代码——先根据规则提取,如果有问题,则使用tldextract

    import tldextract def extract_domain(domain): suffix = {'.com','.la','.io', '.co', '.cn','.info', '. ...

  5. Laravel-redis-订阅发布

    Laravel-redis-订阅发布 标签(空格分隔): php Redis订阅发布 理解订阅发布: publish:将信息 message 发送到指定的频道 channel publish test ...

  6. express+模板引擎构建项目时遇到的几个小问题

    1.启动项目/调试项目 项目启动用:npm start 由于每次更改路由代码后必须重启服务才可以看效果,所以为了达到热加载的效果我们安装 supervisor:全局安装也可以: npm install ...

  7. POJ 3320 Jessica's Reading Problem (尺取法,时间复杂度O(n logn))

    题目: 解法:定义左索引和右索引 1.先让右索引往右移,直到得到所有知识点为止: 2.然后让左索引向右移,直到刚刚能够得到所有知识点: 3.用右索引减去左索引更新答案,因为这是满足要求的子串. 4.不 ...

  8. C#生成高清缩略图 (装在自OPEN经验库)

    来源 http://www.open-open.com/lib/view/open1389943861320.html 代码如下实现图片的高清缩略图 /// <summary> /// 为 ...

  9. Android Studio 开发安卓软件时下载的工程项目 Sync with gradle 失败

    Sync with gradle 失败的原因有很多,其中很多时候会遇到下载下来的工程同步失败,目前的经验来看下载的工程同步失败均是由于下图中的两个配置其中某个缺少了 google() 或者 jcent ...

  10. 解决Windows下git需要每次都要ssh-add的问题

    顽皮的很: 不知道怎么回事,每次打开git提交代码都需要ssh-add一下秘钥才可以正常提交: 不然就报错权限之类的问题: 怎么才能更方便一些? 卸了重装!我没试... 再或者是在 git 的安装目录 ...