Beginners learn Python (13): basic data structure (Dictionary)

Life is short, I choose Python

Previous portal

Little white learning Python (1): the beginning

Little white learning Python (2): basic data types (I)

Python (3): basic data type (2)

Python (4): basic operation of variables

Python (5): basic operators (1)

Python (6): basic operator (2)

Xiao Bai Xue Python (7): basic process control (Part one)

Python (8): basic process control (2)

Python (9): basic data structure (list) (I)

Python (10): basic data structure (list) (2)

Python (11): basic data structure (tuple)

Python (12): basic data structure (Dictionary) (I)

Dictionary built-in functions & Methods

(if you are looking at the emoticon bag, you can quit, this emoticon bag)

Dictionaries provide many built-in methods for manipulating dictionaries.

The old rule is to read the code and introduce it at the same time.

dict.keys()

Function: returns an iterator, which can be converted to a list using list(), which contains all key s.

dict1 = {'name': 'geekdigging', 'age': 2}

print(dict1.keys())
print(list(dict1.keys()))
print(type(list(dict1.keys())))

The results are as follows:

dict_keys(['name', 'age'])
['name', 'age']
<class 'list'>

dict.values()

Function: returns an iterator that can be converted to a list using list(), which contains all value s.

print(dict1.values())
print(list(dict1.values()))
print(type(list(dict1.values())))

The results are as follows:

dict_values(['geekdigging', 2])
['geekdigging', 2]
<class 'list'>

dict.items()

Function: return traversable (key, value) tuple array with list.

print(dict1.items())
print(list(dict1.items()))
print(type(list(dict1.items())))

The results are as follows:

dict_items([('name', 'geekdigging'), ('age', 2)])
[('name', 'geekdigging'), ('age', 2)]
<class 'list'>

dict.get(key, default=None)

Function: returns the value of the specified key. If the value is not in the dictionary, it returns the default(None) value.

print(dict1.get('name'))
print(dict1.get('geekdigging'))

The results are as follows:

geekdigging
None

Because geekdigging is not the key in the dictionary, the default value of None is returned.

dict.pop(key[,default])

Function: delete the value corresponding to the key given in the dictionary, and the return value is the deleted value. The key value must be given.

print(dict1.pop('age'))
print(dict1)

The results are as follows:

2
{'name': 'geekdigging'}

dict.setdefault(key, default=None)

Function: similar to get(), but if the key does not exist in the dictionary, the key will be added and the value will be set to default.

dict1.setdefault('age')
print(dict1)

The results are as follows:

{'name': 'geekdigging', 'age': None}

dict.update(dict2)

Function: update the key / value pair of dict2 to dict.

dict2 = {'sex': 'male'}
dict1.update(dict2)
print(dict1)

The results are as follows:

{'name': 'geekdigging', 'age': None, 'sex': 'male'}

dict.clear()

Function: delete all elements in the dictionary.

Note: delete all elements in the dictionary, not the dictionary.

dict2.clear()
print(dict2)

The results are as follows:

{}

dict.copy()

Function: returns a shallow copy of a dictionary.

Note: the copy here is shallow, and students with programming experience can understand it easily. The inexperienced students listen to me slowly. The shallow copy essentially copies a quote, not the whole content. For example, I have an apple, and the shallow copy is for Xiaoming. I bit my apple, but Xiaoming found that his apple was also bitten.

dict3 = {'name': 'geekdigging', 'age': [1, 2, 3]}
# Shallow copies: referencing objects
dict4 = dict3
print(id(dict3))
print(id(dict4))
# Shallow copy: deep copy parent (primary directory), child (secondary directory) do not copy, or reference
dict5 = dict3.copy()
dict3['age'].remove(1)
print(dict3)
print(dict5)
print(id(dict3))
print(id(dict5))

The results are as follows:

2418990911064
2418990911064
{'name': 'geekdigging', 'age': [2, 3]}
{'name': 'geekdigging', 'age': [2, 3]}
2418990911064
2418991599688

This example is a little difficult to understand. Don't worry. Take your time and enjoy it.

If I want to copy everything deeply, what should I do?

The copy module can be introduced.

import copy

dict3 = {'name': 'geekdigging', 'age': [1, 2, 3]}
dict6 = copy.deepcopy(dict3)
dict3['age'].remove(1)
print(dict3)
print(dict6)
print(id(dict3))
print(id(dict6))

The results are as follows:

{'name': 'geekdigging', 'age': [2, 3]}
{'name': 'geekdigging', 'age': [1, 2, 3]}
2418991602168
2418991602008

For the first time, the above two examples are not easy to understand. If there is any problem in understanding, you can ask me in public address.

As a matter of fact, let's have another BB sentence. Please practice the sample code yourself.

Sample code

All the code in this series will be put on Github and Gitee, which are convenient for you to access.

Sample code Github

Sample code - Gitee

If my article is helpful to you, please scan the code to pay attention to the author's public address: get the latest dry goods push:

Keywords: Python github Programming

Added by Admiral S3 on Mon, 04 Nov 2019 03:10:18 +0200