DictionaryLOGIN

Dictionary

After previous study, we can know that list and tuple can be used to represent ordered sets. In our previous example, we used list to store the user's nickname

user=['liangdianshui','twowater','两点水']

If we need to also store the user's account number Record it?

Using a list can be solved like this:

user=[['liangdianshui','111111'],['twowater','222222'],['两点水','333333']]

But it is inconvenient to express it this way, and it is difficult to find the corresponding nickname based on the nickname, and the longer the list, the longer it takes; at this time, It can be represented by dict (dictionary). Python has a built-in dictionary (dict). The full name of dict is dictionary, which is equivalent to the map in JAVA. It uses key-value (key-value) storage and has extremely fast search speed.

user={'liangdianshui':'111111' ,'twowater':'222222' ,'两点水':'333333'}

1. Creation of dict (dictionary)

Dictionary is another variable container model and can store any type of object.

Each key-value (key=>value) pair in the dictionary is separated by a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in curly braces ({}) , the format is as follows:

dict = {key1 : value1, key2 : value2 }

Note: The key must be unique, but the value does not have to be. Values ​​can be of any data type, but keys must be immutable.

Create dict (dictionary) instance:

dict1={'liangdianshui':'111111' ,'twowater':'222222' ,'两点水':'333333'}
dict2={'abc':1234,1234:'abc'}

2. Access dict (dictionary)

#-*-coding:utf-8-*-
dict1={'liangdianshui':'111111' ,'twowater':'222222' ,'两点水':'333333'}
print(dict1)

Output result:

{'liangdianshui': '111111', 'twowater': '222222', '两点水': '333333'}

One thing to note here is: if this key does not exist in the dictionary, an error will be reported.

3. Modify dict (dictionary)

The way to add new content to the dictionary is to add new key/value pairs, modify or delete existing key/value pairs

#-*-coding:utf-8-*-
dict1={'liangdianshui':'111111' ,'twowater':'222222' ,'两点水':'333333'}
print(dict1)
# 新增一个键值对
dict1['jack']='444444'
print(dict1)
# 修改键值对
dict1['liangdianshui']='555555'
print(dict1)

Output results:

{'liangdianshui': '111111', 'twowater': '222222', '两点水': '333333'}
{'liangdianshui': '111111', 'twowater': '222222', '两点水': '333333', 'jack': '444444'}
{'liangdianshui': '555555', 'twowater': '222222', '两点水': '333333', 'jack': '444444'}

4. Delete dict (dictionary)

You can delete an element in dict (dictionary) through del , can also delete dict (dictionary)

All elements in the dictionary can be cleared by calling the clear() method

#-*-coding:utf-8-*-
dict1={'liangdianshui':'111111' ,'twowater':'222222' ,'两点水':'333333'}
print(dict1)
# 通过 key 值,删除对应的元素
del dict1['twowater']
print(dict1)
# 删除字典中的所有元素
dict1.clear()
print(dict1)
# 删除字典
del dict1

The output result:

{'liangdianshui': '111111', 'twowater': '222222', '两点水': '333333'}
{'liangdianshui': '111111', '两点水': '333333'}
{}

5, Things to note when using dict (dictionary)

(1) dict (dictionary) does not allow a key to be created twice, but if it appears when creating a dict (dictionary) If a key value is assigned twice, the last assigned value will prevail

For example:

#-*-coding:utf-8-*-
dict1={'liangdianshui':'111111' ,'twowater':'222222' ,'两点水':'333333','twowater':'444444'}
print(dict1)
print(dict1['twowater'])

The output result:

{'liangdianshui': '111111', 'twowater': '444444', '两点水': '333333'}
444444

( 2) dict (dictionary) keys must be immutable, but keys can be used as numbers, strings or tuples, but lists cannot be used

For example:

#-*-coding:utf-8-*-
dict1={'liangdianshui':'111111' ,123:'222222' ,(123,'tom'):'333333','twowater':'444444'}
print(dict1)

Output results:

{'liangdianshui': '111111', 123: '222222', (123, 'tom'): '333333', 'twowater': '444444'}

(3) The order in which dict is stored internally has nothing to do with the order in which keys are put in.

Compared with list, dict has the following characteristics:

The search and insertion speed is extremely fast and will not slow down as the key increases.

It takes up a lot of memory and wastes a lot of memory.

The opposite is true for list:

The search and insertion time increases as the number of elements increases

It takes up little space and wastes very little memory

6. Functions and methods of dict (dictionary)

Methods and functionsDescription
cmp(dict1, dict2)Compare two dictionaries Element
len(dict)Calculate the number of dictionary elements
str(dict) Output the printable string representation of the dictionary
type(variable) Return the input variable type, if the variable is a dictionary, return the dictionary type
dict.clear()Delete all elements in the dictionary
dict.copy()Return a dictionary Copy
dict.values()Return all values ​​in the dictionary as a list
popitem() Randomly returns and deletes a pair of keys and values ​​in the dictionary
dict.items()Returns a traversable (key, value) element as a list Group array




Next Section
submitReset Code
ChapterCourseware