With old Ziko python dictionary, do you remember? _python

Source: Internet
Author: User
Tags hash in python

Dictionary, do you still use this thing now? With the development of the network, fewer people are used. Many people are accustomed to searching on the internet, not only the web version, but also the mobile version of a variety of dictionaries. I used to use a small "Xinhua dictionary".

"Xinhua Dictionary" is the first modern Chinese dictionary in China. The earliest name was "Wooke dictionary", but could not be completed. Since 1953, the general notices has been fully adopted by the Wooke dictionary. Published since 1953, after repeated revisions, but the 1957 commercial Press published the "Xinhua dictionary" as the first edition. Originally by the Xinhua Dictionary Society, 1956 merged into the Chinese Academy of Sciences Institute of Language (now the Institute of Language Research Academy of Social Sciences) dictionary editing room. The Xinhua Dictionary is published by the commercial press. After several generations of hundreds of experts and scholars more than 10 large-scale revision, reprint 200 times. Become the highest circulation dictionary in the History of world publishing.
The dictionary here is not for catching up. Instead, remind reader to think about how we use the dictionary: First look at the index (whether it is pinyin or the character), and then find the appropriate content through the index.

This method can quickly find the target.

In Python, there is also a kind of 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 need to know how to establish dict and its related property methods.

Because of the previous foundation, so learning this can be accelerated.

It has been suggested that reader a good way to learn and explore, for example, about the properties of STR, which can be used in interactive mode:

Copy Code code as follows:

>>>help (str)

Will get all the relevant content.

Now, with Dir, you can get the same result. 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 ', ' itervalues ', ' Keys ', ' pops ', ' popitem ', ' setdefault ', ' Update ', ' values ', ' viewitems ', ' Viewkeys ', ' Viewvalues ']


Start with __ (double underline). Look at the back. If you want to get a closer look, you can do this:

Copy Code code as follows:

>>> Help (Dict.values)

Then appear:

Copy Code code as follows:

Help on Method_descriptor:

VALUES (...)
D.values ()-> list of D ' s values
(end)


This shows the use of values as a built-in function. Tap the Q key on the keyboard to return.

Overview

The dict in Python have the following characteristics:

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, there is no relationship, through the following learning, especially through a variety of experiments, you can understand.

Create Dict

The way to create Dict is far more than the previous int/str/list, why is there more? General law is complex point of things will have a variety of channels to generate, this is from a safe and convenient point of view.

Method 1:

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

Copy Code code as follows:

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

Create a dict with content.

Copy 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 front name is called a key, and the following qiwsir is the corresponding value (value) of the preceding key. In a dict, the keys are unique and cannot be duplicated; values correspond to keys, and values can be duplicated. Key values (:) semicolons in English, each pair of key values separated by commas (,) in English.

Copy 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 from an empty dict is demonstrated:

>>> mydict = {}
>>> mydict
{}
>>> mydict["Site" = "Qiwsir.github.io"
> >> mydict[1] = +
>>> mydict[2] = "python"
>>> mydict["Name" = ["Zhangsan", "Lisi", " Wangwu "]
>>> mydict
{1:80, 2: ' Python ', ' site ': ' Qiwsir.github.io ', ' name ': [' Zhangsan ', ' Lisi ', ' Wangwu ']}

>>> mydict[1] = #如果这样, is to modify the value of this key
>>> mydict
{1:90, 2: ' Python ', ' site ': ' Qiwsir.github.io ', ' name ': [' Zhangsan ', ' Lisi ', ' Wangwu ']}

Method 2:

>>> name = (["A", "Google"],["second", "Yahoo"])   #这是另外一种数据类型, called tuples, followed by
>>> website = Dict (name)
>>> website
{' second ': ' Yahoo ', ' I ': ' 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 '}

It is to be recalled that this approach is to create a new dict.

Accessing the value of Dict

Because Dict stores data as a key-value pair, you can get a value if 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 site
qiwsir.github.io

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

Reader can be compared with list. If we access the elements in the list, we can get the index worth (List[i]), and if the machine is to travel, you can use the For statement. Review:

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

So is it possible for dict to iterate with the for statement? Of course, take a look at the example:

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

Knowledge

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

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

1. Add pairing to associative array
2. Remove the pairing from the associative array
3. Modify the pairing in an associative array
4. Find the pairing according to the known key
The dictionary problem is to design a data structure that can have associative array characteristics. A common way to solve dictionary problems is to use a hash table, but in some cases, you can also use an array of addresses 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 implementation of the associative array support.
What is a hash table? There's a lot more to the hash table, just a description of the concept, and more to 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 through a function calculation to a location in the table, which speeds up the lookup. This mapping function is called a hash function, and the array of records is called a hash table.

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.