With the old Ziko python dictionary, do you remember?

Source: Internet
Author: User
Dictionary, do you still use this thing now? With the development of the network, the use of fewer people. Many people are accustomed to search on the Internet, not only the web version, but also has a mobile version of the various dictionaries. I used to use a small "Xinhua dictionary".

"Xinhua Dictionary" is the first modern Chinese Dictionary of China. The earliest name was called "Wooke dictionary", but it was not compiled and completed. Since 1953, the general notices has been re-compiled with the full use of the Wooke dictionary. Published since 1953, after repeated revisions, but the 1957 commercial Press published "Xinhua dictionary" as the first edition. Originally written by Xinhua Dictionary Society, 1956 merged into the Chinese Academy of Sciences Institute of Languages (now China Academy of Social Sciences) dictionary editorial office. The Xinhua Dictionary is published by the commercial press. After several generations of experts and scholars more than 10 times a large-scale revision, reprint more than 200. A dictionary of the highest circulation in the history of the world's publications so far.
The dictionary here is not for catching up. Instead, remind crossing to think about how we use the dictionary: Check the index first (whether it's pinyin or the radicals), and then find the content by index.

This method can quickly find the target.

In Python, there is also a data similar to this, not only close, the name of this data is called dictionary, translation is a dictionary, similar to the previous int/str/list, this type of data name is: Dict

Depending on the management, you should know how to establish dict and how it relates to attribute methods.

Because we already have the foundation of the previous, so, learn this can be accelerated.

It was suggested that crossing a good learning approach, such as to understand Str's related attribute methods, which can be used in interactive mode:

Copy the Code code as follows:


>>>help (str)


Will get all the relevant content.

Now for another, using Dir, you can get the same results. Just a little bit simpler. In interactive mode:

>>> dir (dict) [' __class__ ', ' __cmp__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __doc__ ', ' __eq__ ', ' __ Format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __iter__ ', ' __le__ ', ' __len_ _ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __setitem__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' clear ', ' copy ', ' Fromkeys ', ' get ', ' has_key ', ' Items ', ' iteritems ', ' Iterkeys ', ' Iterv ' Alues ', ' Keys ', ' Pop ', ' Popitem ', ' setdefault ', ' Update ', ' values ', ' viewitems ', ' Viewkeys ', ' viewvalues ']


Start with __ (double underline). Look at the back. If you want to learn more, you can:

Copy the Code code as follows:


>>> Help (Dict.values)


Then appears:

Copy the Code code as follows:


Help on Method_descriptor:

VALUES (...)
D.values (), List of D ' s values
(END)


This is where the use of the built-in function of values is shown. Tap the Q key on the keyboard to return.

Overview

The dict in Python has the following features:

Dict is variable.
Dict can store any number of Python objects
Dict can store any Python data type
Dict stores data in the form of: Key:value, which is the "key: Value" pair, and each key is unique.
Dict is also known as an associative array or hash table.
The above articles, if not very understanding, also has no relationship, through the following study, especially through a variety of experiments, will be able to understand.

Create Dict

The way to create Dict is far more than the previous int/str/list, why? The general rule is the complex point of things will have a variety of channels to generate, this is from a safe and convenient angle to consider it.

Method 1:

Create an empty dict, this empty dict, you can add something to it later.

Copy the Code code as follows:


>>> mydict = {}
>>> mydict
{}


Create a dict with content.

Copy the Code code as follows:


>>> person = {"Name": "Qiwsir", "Site": "Qiwsir.github.io", "language": "Python"}
>>> person
{' name ': ' Qiwsir ', ' language ': ' Python ', ' site ': ' Qiwsir.github.io '}


"Name": "Qiwsir" is a key-value pair, the preceding name is called the key, and the following qiwsir is the value of the previous key. In a dict, the key is unique, cannot be duplicated, and the value corresponds to the key, and the value can be repeated. Between key values (:) semicolon in English, separated by a comma (,) between each pair of key values.

Copy the Code code as follows:


>>> person[' name2 ']= "Qiwsir" #这是一种向dict中增加键值对的方法
>>> person
{' name2 ': ' Qiwsir ', ' name ': ' Qiwsir ', ' language ': ' Python ', ' site ': ' Qiwsir.github.io '}


As shown below, the process of adding content starting with an empty dict is demonstrated:

>>> mydict = {}>>> mydict{}>>> mydict["site"] = "Qiwsir.github.io" >>> mydict[1] = 80 >>> mydict[2] = "python" >>> mydict["name"] = ["Zhangsan", "Lisi", "Wangwu"]>>> mydict{1:80, 2 : ' Python ', ' site ': ' Qiwsir.github.io ', ' name ': [' Zhangsan ', ' Lisi ', ' Wangwu ']}>>> mydict[1] = #如果这样, modify this key Value >>> mydict{1:90, 2: ' Python ', ' site ': ' Qiwsir.github.io ', ' name ': [' Zhangsan ', ' Lisi ', ' Wangwu ']}

Method 2:

>>> name = (["First", "Google"],["second", "Yahoo"])   #这是另外一种数据类型, referred to as tuples, followed by >>> website = dict ( Name) >>> website{' second ': ' Yahoo ', ' first ': ' Google '}

Method 3:

The difference between this method and the above is the use of Fromkeys

>>> website = {}.fromkeys ("third", "forth"), "Facebook") >>> website{' forth ': ' Facebook ', ' third ': ' Facebook '}

To be reminded, this approach is to create a new dict.

Accessing the value of Dict

Because Dict stores data in the form of a key-value pair, you can get the value as long as you know the key. This is essentially a mapping relationship.

>>> person{' name2 ': ' Qiwsir ', ' name ': ' Qiwsir ', ' language ': ' Python ', ' site ': ' Qiwsir.github.io '}>>> person[' name '] ' qiwsir ' >>> person[' language '] ' python ' >>> site = person[' site ']>>> print Siteqiwsir.github.io

As mentioned above, by the key can increase the value in the Dict, through the key can change the value in the Dict, through the key can also access the value in the Dict.

Crossing can be compared with the list. If we access the elements in the list, it is worth the index (List[i]), and if the machine is to be visited, it can be used for a statement. Review:

>>> person_list = ["Qiwsir", "Newton", "Boolean"]  >>> for name in Person_list:   ... Print name ... Qiwsirnewtonboolean

So, can dict also use a For statement to iterate? Sure, let's take a look at examples:

>>> person{' name2 ': ' Qiwsir ', ' name ': ' Qiwsir ', ' language ': ' Python ', ' site ': ' Qiwsir.github.io '}>>> For key in person: ...   Print Person[key] ... Qiwsirqiwsirpythonqiwsir.github.io

Knowledge

What is an associative array? The following explanations are from Wikipedia

In computer science, associative arrays (English: Associative array), also known as map (map), dictionary (Dictionary), are an abstract data structure that contains an ordered pair similar to (key, value). An ordered pair in an associative array can be duplicated (such as multimap in C + +) or not duplicated (such as a map in C + +).
This data structure contains the following common operations:

1. Adding a pairing to an associative array
2. Deleting a pairing from an associative array
3. Modify the pairing within the associative array
4. Find the pairing based on the known key
The problem of dictionaries is to design a data structure that can have associative array characteristics. The common way to solve dictionary problems is to use hash lists, but in some cases, you can also use address arrays directly, or binary trees, and other structures.
Many programming languages have built-in basic data types that provide support for associative arrays. The content-addressable memory is a hardware-level support for associative arrays.
What is a hash table? The description of the hash table is much more, it just intercepts the concept, more can be read on Wikipedia.

A hash table, also known as a hash table, is a data structure that accesses the memory storage location directly based on the keyword (key value). That is, it accesses the record by mapping the key value to a position in the table through the calculation of a function, which speeds up the lookup. This mapping function is called a hash function, and the array that holds the record is called a hash list.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.