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

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

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

  2. 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 ...

  3. Think Python - Chapter 11 - Dictionaries

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

  4. Arrays, Hashtables and Dictionaries

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

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

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

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

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

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

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

  8. 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.  ...

  9. 【RF库Collections测试】Dictionaries Should Be Equal

    Name:Dictionaries Should Be EqualSource:Collections <test library>Arguments:[ dict1 | dict2 | ...

  10. Dictionaries and tuples

    Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-val ...

随机推荐

  1. POJ 3695

    可以用容斥原理来求.求两个矩形的并的时候可以使用条件 x1=max(p.x1,q.x1);y1=max(p.y1,q.y1);x2=min(p.x2,q.x2);y2=min(p.y2,q.y2); ...

  2. C++实现页码数字统计

    #include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #incl ...

  3. 走进windows编程的世界-----入门篇

    1   Windows编程基础 1.1Win32应用程序基本类型 1)  控制台程序 不须要完好的windows窗体,能够使用DOS窗体方式显示 2)  Win32窗体程序 包括窗体的程序,能够通过窗 ...

  4. 算法 - 求一个数组的最长递减子序列(C++)

    //************************************************************************************************** ...

  5. hbase的命令

    1.1. 命令 名称 命令表达式 创建表 create '表名', '列族名1','列族名2','列族名N' 查看所有表 list 描述表 describe  ‘表名’ 判断表存在 exists  ' ...

  6. 循环访问 TreeView 控件的所有节点

    创建测试每个节点的递归过程 . private void PrintRecursive(TreeNode treeNode) { // Print the node. System.Diagnosti ...

  7. sql server 更新满足条件的某一条记录

    上图数据:SNum为”18004XXXXX000001K2GW 4000 L1C“,OffLineStation为“OP1010”的有两条数据,当where条件中为上述两者时会同时更新这两条数据,并不 ...

  8. BAT 解密(四):配置中心、服务中心、异步技术细节

    在系列文章的第二篇文章< BAT解密(二):聊聊业务如何驱动技术发展 >中我们深入分析了互联网业务发展的一个特点:复杂性越来越高.复杂性增加的典型现象就是系统越来越多,当系统的数量增加到一 ...

  9. shell-3.bash的基本功能:输入输出重定向

    1. 2. 3. 4.

  10. 如何给table的指定td进行css样式改变

    td:nth-child(){background-color:#; color:#fff;}/*把第3个td的背景设为黑色*/ :nth-child()不止可以给table指定样式 p标签页是可以的 ...