Dictionaries
A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type. You can think of a dictionary as a mapping between a set of indices and a set of values. Each index, which is called a key, corresponds to a value. The association of a key and a value is called a key-value pair or sometimes an item.
As an example, we will build a dictionary that maps from English words to Spanish words, so the keys and values are all strings. The squiggly-brackets, {}, represent an empty dictionary. To add items to the dictionary, you can use square brackets.
The key-value pairs are not in order, but that’s not a problem because the elements of a dictionary are never indexed with integer indices. Instead, you use the keys to look up the corresponding values:
If the key isn’t in the dictionary, you get an exception. The len function works on dictionaries; it returns the number of key-value pairs.
The in operator works on dictionaries; it tells you whether something appears as a key in the dictionary (appearing as a value is not good enough).
The in operator uses different algorithms for lists and dictionaries. For lists, it uses a search algorithm. As list gets longer, the search time gets longer in direct proportion. For dictionaries, Python uses an algorithm called hashtable that has a remarkable property: the in operator takes about the same amount of time no matter how many items there are in a dictionary.
from Thinking in Python
Dictionaries的更多相关文章
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
- 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 ...
- Think Python - Chapter 11 - Dictionaries
Dictionaries A dictionary is like a list, but more general. In a list, the indices have to be intege ...
- Arrays, Hashtables and Dictionaries
Original article Built-in arrays Javascript Arrays(Javascript only) ArrayLists Hashtables Generic Li ...
- IOS学习之路十九(JSON与Arrays 或者 Dictionaries相互转换)
今天写了个json与Arrays 或者 Dictionaries相互转换的例子很简单: 通过 NSJSONSerialization 这个类的 dataWithJSONObject: options: ...
- 出现“java.lang.AssertionError: SAM dictionaries are not the same”报错
运行一下程序时出现“java.lang.AssertionError: SAM dictionaries are not the same”报错 java -jar picard.jar SortVc ...
- [Python] 03 - Lists, Dictionaries, Tuples, Set
Lists 列表 一.基础知识 定义 >>> sList = list("hello") >>> sList ['h', 'e', 'l', ' ...
- A writer of dictionaries,a harmless druge.
Nine Years for A and B By Christopher Ricks Dr. Johnson was the greatest man who made a dictionary. ...
- 【RF库Collections测试】Dictionaries Should Be Equal
Name:Dictionaries Should Be EqualSource:Collections <test library>Arguments:[ dict1 | dict2 | ...
- Dictionaries and tuples
Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-val ...
随机推荐
- POJ 1286
Burnside定理. 可以用Euler函数优化. #include <iostream> #include <cstdio> #include <cstring> ...
- HDU5411CRB and Puzzle(矩阵高速幂)
题目链接:传送门 题意: 一个图有n个顶点.已知邻接矩阵.问点能够反复用长度小于m的路径有多少. 分析: 首先我们知道了邻接矩阵A.那么A^k代表的就是长度为k的路径有多少个. 那么结果就是A^0+A ...
- Popupwindow 显示, 其它背景变暗。 并加上点击事件 ~ (用于记录)
public class MainActivity extends Activity implements OnClickListener { protected int mScreenWidth; ...
- Sambaserver搭建
1. 安装Samba及相关包 $ sudo apt-getinstall samba samba-common smbfspython-glade2system-config-samba 2. 创建共 ...
- validation-api参数校验
这里针对springboot项目结构 maven添加: <dependency> <groupId>javax.validation</groupId> <a ...
- Elasticsearch之重要核心概念(cluster(集群)、shards(分配)、replicas(索引副本)、recovery(据恢复或叫数据重新分布)、gateway(es索引的持久化存储方式)、discovery.zen(es的自动发现节点机制机制)、Transport(内部节点或集群与客户端的交互方式)、settings(修改索引库默认配置)和mappings)
Elasticsearch之重要核心概念如下: 1.cluster 代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的.es的一个概念就是 ...
- Swift学习笔记(8):闭包
目录: 基本语法 尾随闭包 值捕获 自动闭包 闭包是自包含的函数代码块,闭包采取如下三种形式之一: ・全局函数是一个有名字但不会捕获任何值的闭包 ・嵌套函数是一个有名字并可以捕获其封闭函数域内值的闭包 ...
- C++之易混淆知识点三---算法分析
最近复习算法,感到有一丝丝忘记的困惑,赶紧记下来... 一.分治法 分治法的思想就是“分而治之”,很明显就是将规模比较庞大.复杂的问题进行分治,然后得到多个小模块,最好这些小模块之间是独立的,如果这些 ...
- IP地址的正则表达式写法
这里讲的是IPv4的地址格式,总长度 32位=4段*8位,每段之间用.分割, 每段都是0-255之间的十进制数值. 将0-255用正则表达式表示,可以分成一下几块来分别考虑: 取值区间 特点 正则写法 ...
- js 拼接字符串,table等
var userTableStr=''; userTableStr +='<table width="750" height="33" border=&q ...