Python basic grammar learning-dictionary

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Continue to learn the basic syntax of python this week, the content is from Chapter 6 to Chapter 9


1. Dictionary

A dictionary is actually a series of key-value pairs, each key is associated with a value, and the value can be a number, a string, a list, a tuple, or a dictionary.

1. Creation of dictionary

dictionary used {}to represent

alien_0 = {
    
    'color':'green','points':5}

The above code creates a dictionary

2. Access the value of the dictionary

alien_0['color']You can access the value corresponding to the key by using

3. Modify the value of the dictionary

use

alien_0['color'] = 'red'

You can modify the value corresponding to the 'color' key.

4. Delete the key-value pair

Use del alien_0['color']to delete the corresponding key-value pair

5. Use get() to access dictionary values

Use help(dict.get)can print out the usage of get
insert image description here
as shown in the figure, which means that the incoming parameter is key, if the key is in the dictionary, return the value corresponding to the key, if not, return None

print(alien_0.get("1"))
None
print(alien_0.get("points"))
5

6. Traverse the dictionary

First define a dictionary

user_0 = {
    
    'usename':'efermi','first':'enrico','last':'fermi'}

If you want to iterate over this dictionary

for key,value in user_0.items():
    print("value:{value}".format(value = value))
    print("ket:{key}".format(key = key))

The result is as follows

value:efermi
ket:usename
value:enrico
ket:first
value:fermi
ket:last

7.dict.items()

The above traversal dictionary uses items()the function of the dictionary. I just started using it in IDLE. I help(dict.items())
insert image description here
don’t understand it very well. I went to the official python document to check the following. The official document’s explanation is as follows.
insert image description here
It means that the key-value pair of the dictionary will be returned
and I will print it in the code. After a while, dict.items()
insert image description here
you can see that a list should be returned, which is an iterable object, and the elements of each list are a tuple, so use the above method to traverse the dictionary

8.dict.keys()和dict.values()

Both are explained as follows

help(dict.values)
Help on method_descriptor:

values(...)
    D.values() -> an object providing a view on D's values

help(dict.keys)
Help on method_descriptor:

keys(...)
    D.keys() -> a set-like object providing a view on D's keys

The description will return an object, which is all the values ​​and keys of the dictionary.
These two methods are distinguished from the previous items() method.

9. About the collection set()

Collections and dictionaries are easy to confuse, and dictionaries are also used when defining {}, but there are no key-value pairs, and there are no repeated elements in the collection, so if you need to deduplicate a list, you can use, set()for example

a = [1,2,3,4,5,1]
b = set(a)
b
{
    
    1, 2, 3, 4, 5}

It can be seen that b has successfully deduplicated a.


Summarize

This week, I learned the dictionary of python basic grammar

Guess you like

Origin blog.csdn.net/weixin_47250738/article/details/130559434