zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    linux redis 安装和链接,,,
    ppt转化pdf
    跨服务器 同步数据
    字典表相关
    代码重构,空间换时间,dictionary 不要用object ,需明确指定类型
    stringbuilder for test performance 性能 update 性能
    nvarchar 和varchar 在len下一致,datalength下nvarchar翻倍
    android GradLayout实现计算器
    屏幕录制GIF动画工具
    android SharedPreferences 简单的数据存储
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3842300.html
Copyright © 2011-2022 走看看