Do you still remember to learn the Python dictionary from old Qi ?, Qi xue python

Source: Internet
Author: User

Do you still remember to learn the Python dictionary from old Qi ?, Qi xue python

Dictionary. Are you still using this item? With the development of the network, fewer and fewer users are used. Many people are used to searching on the Internet, not only the web version, but also the various dictionaries of the mobile version. I used a small Xinhua Dictionary.

Xinhua Dictionary is China's first modern Chinese dictionary. The earliest name was Wu Kee dictionary, but it was not completed. Since 1953, he began to re-compile the data. His examples use the Wu Kee dictionary. It was published in 1953 and has been revised repeatedly. However, the first version of "Xinhua Dictionary" published by the Business Printing Museum in 1957 is used. It was originally compiled by Xinhua Dictionary club and was incorporated into the dictionary editing room of the Chinese Emy of Sciences Language Research Institute (now the Chinese Emy of Social Sciences Language Research Institute) in 1956. Xinhua Dictionary is published by the business press. After more than 10 large-scale revisions by several generations of hundreds of experts, scholars, and more than 200 reprints. It has become the highest circulation dictionary in the history of World Publishing so far.
Here we talk about the dictionary, not for the sake of the old. Instead, they remind the readers to think about how to use the dictionary: first query the index (whether in pinyin or by-text search), and then find the corresponding content through the index.

This method can quickly find the target.

In python, there is also a similar type of data, which is not only similar. The data name is called dictionary, which is translated as a dictionary, similar to the previous int/str/list, this type of data name is: dict

Based on management, You Need To Know How To establish dict and Its Property methods.

Because we already have the foundation, learning this can be accelerated.

We have previously suggested reading the official website a good way to learn and explore. For example, if you want to know the str attribute methods, you can use them in interactive mode:

Copy codeThe Code is as follows:
>>> Help (str)

All related content will be obtained.

Now you can use dir to get the same result. It's just a little 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', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']


It must start with _ (Double underline. Next. For more information, see:

Copy codeThe Code is as follows:
>>> Help (dict. values)

Then:

Copy codeThe Code is as follows:
Help on method_descriptor:

Values (...)
D. values ()-> list of D's values
(END)

That is, the usage of the built-in function values is displayed here. Press the q key on the keyboard to return.

Overview

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 a key: value pair. Each key is unique.
Dict is also called an associated array or hash table.
The above articles can be understood through the following learning, especially through various experiments.

Create dict

In other words, the method for creating dict is far more than the previous int/str/list. Why? The general rule is that complicated things are generated through multiple channels. This is also from the perspective of security and convenience.

Method 1:

Create an empty dict, which can be used later.

Copy codeThe Code is as follows:
>>> Mydict = {}
>>> Mydict
{}

Create a dict with content.

Copy codeThe Code is 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 previous name is called a key, and the qiwsir is the value corresponding to the previous key ). In a dict, keys are unique and cannot be repeated. values correspond to keys and values can be repeated. Key values are separated by semicolons (:), and each pair of key values is separated by commas.

Copy codeThe Code is as follows:
>>> Person ['name2'] = "qiwsir" # This is a method to add a key-value pair to dict.
>>> Person
{'Name2': 'qiwsir ', 'name': 'qiwsir', 'language ': 'python', 'SITE': 'qiwsir. github. io '}

The following shows how to add content from an empty dict:

>>> 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': ['hangsan', 'lisi', 'hangzhou'] >>> mydict [1] = 90 # If so, modify the key value >>> mydict {1: 90, 2: 'python', 'SITE': 'qiwsir. github. io ', 'name': ['hangsan', 'lisi', 'hangzhou']}

Method 2:

>>> Name = (["first", "Google"], ["second", "Yahoo"]) # This is another data type, called a tuples, later I will talk about >>> website = dict (name) >>> website {'second': 'yahoo ', 'First': 'Google '}

Method 3:

The difference with this method is that fromkeys

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

It should be noted that this method is to create a new dict.

Access dict Value

Because dict stores data in the form of key-value pairs, you only need to know the key to get the value. This is essentially a ing 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, a key can be used to increase the value in dict, change the value in dict, and access the value in dict.

The viewer can compare it with the list. If we access the elements in the list, we can use the index value (list [I]). If we want the machine to visit the list, we can use the for statement. Review:

>>> person_list = ["qiwsir","Newton","Boolean"]  >>> for name in person_list:...   print name... qiwsirNewtonBoolean

So can dict be cyclically accessed using the for statement? Of course, let's look at the example:

>>> 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 associated array? The following explanations are from Wikipedia:

In computer science, the associated Array (English: Associative Array), also known as Map, and Dictionary, is an abstract data structure, which contains similar (Key, value). An ordered pair in an associated array can be repeated (for example, multimap in C ++) or not (for example, map in C ++ ).
This data structure includes the following common operations:

1. Add a pair to the correlated Array
2. delete a pair from the joined Array
3. Modify the matching in the joined Array
4. Search for matching based on known keys
The dictionary problem is to design a data structure that can have the attribute of associating arrays. A common method to solve the dictionary problem is to use a hash, but in some cases, you can also directly use an array with addresses, binary trees, and other structures.
Many programming languages have built-in basic data types that provide support for correlated arrays. Content-addressable memory supports the associated array on the hardware layer.
What is a hash table? There are many descriptions of hash tables. Here we only extract the concept description. For more information, see Wikipedia.

A Hash table (also called a Hash table) directly accesses the data structure in the memory storage location based on the Key value. That is to say, It maps the key value to a position in the table through the calculation of a function to access the record, which accelerates the search speed. This ing function is called a hash function, and the array that stores records is called a hash function.




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.