A Dictionary of Python

Source: Internet
Author: User

1. A simple dictionary

Alien_0 = {' Color ': ' green ', ' Points ': 5}
Print (alien_0[' color ')
Print (alien_0[' points ')
The dictionary alien_0 stores the aliens ' colors and points. Use two print statements to access and print this information, such as
is shown below:
Green
5

2. Using a dictionary

(1) in Python, a dictionary is a series of key-value pairs. Each key is associated with a value, and you can use the key to access the
The associated value. The value associated with the key can be a number, a string, a list, or even a dictionary. In fact, you can add any python to the
As a value in the dictionary.
In Python, the dictionary is represented by a series of key-value pairs placed in curly braces {}.

(2) A key-value pair is a value that is associated with two. When you specify a key, Python returns the value associated with it. Use a colon between the key and the value
Separated by a comma separated by a key-value pair

(3) Accessing the values in the dictionary:

To get the value associated with the key, you can specify the dictionary name in turn and the key that is placed inside the square brackets.

(4) Add key-value pairs:

A dictionary is a dynamic structure where you can add key-value pairs at any time. To add a key-value pair, you can specify the dictionary name, in turn,
The key enclosed in square brackets and the associated value.

(5) Create an empty dictionary

When you use a dictionary to store user-supplied data or to write code that automatically generates a large number of key-value pairs, you usually need to first
Defines an empty dictionary. Example: variable name = {} An empty dictionary was created successfully.

(6) Modify the values in the dictionary

To modify a value in a dictionary, you can specify the dictionary name, the key enclosed in square brackets, and the new value associated with the key.

(7) Delete build-value pairs

For information that is no longer needed in the dictionary, you can use the DEL statement to remove the corresponding key-value pairs completely. When using the DEL statement,
You must specify the dictionary name and the key to delete.

Example

Alien_0 = {' Color ': ' green ', ' Points ': 5}
Print (ALIEN_0)
Del alien_0[' points ']
Print (ALIEN_0)

Run results

{' Color ': ' green ', ' Points ': 5}
{' Color ': ' Green '}

(8) A dictionary consisting of similar objects

Favorite_languages = {
' Jen ': ' Python ',
' Sarah ': ' C ',
' Edward ': ' Ruby ',
' Phil ': ' Python ',
}
As you can see, we put a larger dictionary in multiple lines. Each of these keys is the name of a person being investigated,
And each value is the language that the respondents like. When you are sure that you need to use multiple lines to define a dictionary, press ENTER after entering the left curly brace
Key, then indent four spaces on the next line, specify the first key-value pair, and add a comma after it. Then you press again
Enter, the text editor automatically indents subsequent key-value pairs, and the indent amount is the same as the first key-value pair.
After the dictionary is defined, add a closing curly brace to the next line of the last key-value pair and indent four spaces to make it
The keys in the dictionary are aligned. Another good practice is to add a comma after the last key-value pair, for later on the next line
Add a key-value pair to prepare.

3. Traversing a dictionary

(1) Traversal of all key-value pairs

Traversing A For loop for a dictionary, you can declare two variables that store keys and values in key-value pairs.
For both variables, you can use any name, and the second part of the For statement contains the dictionary name and method items ().

Shape as for K, V in User_0.items ()

(2) Traversing all keys in the dictionary

The method keys () is useful when you do not need to use the values in the dictionary.

(3) Sequentially traverse all keys in the dictionary

The dictionary always explicitly records the association between keys and values, but when getting the elements of a dictionary, the fetch order is unpredictable.
This is not a problem, because usually all you want is to get the correct value associated with the key.
One way to return an element in a specific order is to sort the returned key in the For loop. To do this, you can use the letter
Number sorted () to get a copy of the list of keys in a particular order

(4) Traversing all values in the dictionary

If you are interested primarily in the values that the dictionary contains, you can use the method values (), which returns a list of values without
Any key.

Example: for language in Favorite_languages.values ():

This approach extracts all the values in the dictionary without considering duplicates. This may not be a problem when the value involved is very small,
But if the respondents are many, the final list may contain a large number of duplicates. To reject duplicates, you can use the collection (set).
Collections are similar to lists, but each element must be unique

Example: for language in Set (Favorite_languages.values ()):

4. Nesting

(1) List of dictionaries

It is often necessary to include a large number of dictionaries in the list, each of which contains numerous information about a particular object

(2) storing the list in a dictionary

You need to store the list in a dictionary instead of storing the dictionary in a list, and the nesting levels of lists and dictionaries should not be too many.

(3) storing dictionaries in dictionaries

Note that the structure of each user's dictionary is the same, although Python does not have such a requirement, but this makes the embedded
Set of dictionaries is easier to handle. If you indicate that each user's dictionary contains a different key, the code inside the For loop will
More complex.

A Dictionary of Python

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.