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. 自己定义控件-MultipleTextView(自己主动换行、自己主动补齐宽度的排列多个TextView)

    一.功能: 1.传入一个 List<String> 数组,控件会自己主动加入TextView,一行显示不下会自己主动换行.而且把上一行末尾的空白通过拉伸而铺满. 2.配置灵活 <co ...

  2. 在fragment中显示对话框出现异常

    异常类型为:android.view.WindowManager$BadTokenException: Unable to add window 我在fragment中public View onCr ...

  3. Oracle Table Function

    Oracle Table Function在Oracle9i时引入.完美的兼容了view和存储过程的长处: 应用举例: 1.Table()函数: set feedback off create or ...

  4. ES shard unassigned的解决方法汇总

    说下shard出现的几个状态说明: relocating_shards shows the number of shards that are currently moving from one no ...

  5. 【App 开发框架 - App Framework】

    http://edm.mcake.com/mark/jqmboi/#plugins 官网:http://app-framework-software.intel.com/index.php 官方API ...

  6. 访问Storm ui界面,出现org.apache.storm.utils.NimbusLeaderNotFoundException: Could not find leader nimbus from seed hosts ["master" "slave1"]. Did you specify a valid list of nimbus hosts for confi的问题解决(图文详解)

    不多说,直接上干货! 前期博客 apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)(非HA和HA)  问题详情 org.apache.storm.utils.Nimbu ...

  7. js调节图片的亮度

    js调节图片的亮度:(使用CSS3的滤镜) 1.实现点亮图标.熄灭图标的效果 效果图: 页面代码: <!DOCTYPE html> <%@ page language="j ...

  8. 基于localStorage的登录注册

    以下代码,如果有地方有错,请直接指出,我会改进的(只改错误,不改逻辑,因为我自己是不会这样写代码的,这个只适合初学者): <!DOCTYPE html> <html> < ...

  9. Quartz任务调度 服务日志+log4net打印日志+制作windows服务

    引言 现在许多的项目都需要定时的服务进行支撑,而我们经常用到的定时服务就是Quartz任务调度了.不过我们在使用定时Job进行获取的时候,有时候我们就需要记录一下自定义的日志,甚至我们还会对执行定时J ...

  10. DCDCBigBig's first blog @cnblogs~

    其实初二末的时候我就在CSDN上开了博客(主要是为了存模板),但是无奈CSDN的页面真的太辣眼睛了…… 然后我就加入博客园欢快的大家庭啦!!!顺便膜拜巨佬学长%%%(我是蒟蒻不要喷我) 页面设置感谢x ...