I am a front-end nobody, for my own better opportunities, I am trying to learn Python by myself, I hope there are like-minded leaders can communicate

In this article, you will learn about dictionaries in Python, which are like data type objects in JavaScript

The dictionary

1.1 Dictionary definition and access

A dictionary is the equivalent of an object in JS
Dict definitions are defined with {} and consist of key-value pairs.
# variable = {key: value1, key2: value2,... } A key: value pair is an element
# the dictionary key can be a string or a number (int float), not a list
The # value value can be of any type
# 1. Define an empty dictionary
my_dict = {}
my_dict1 = dict(a)print(my_dict, type(my_dict))  # {} <class 'dict'>
print(my_dict1, type(my_dict1))  # {} <class 'dict'>

# 2. Define a dictionary with data
my_dict2 = {'name': 'isaac'.'age': 18.'like': ['learning'.'shopping'.'games'].1: [2.5.8]}
print(my_dict2)  # {' name ':' Isaac ', 'age: 18,' like ': [' learning', 'shopping', 'games'], 1: [2, 5, 8]}


# 3. Access the value of a key value
print(my_dict2['age'])  # 18
print(my_dict2['like'] [1])  # shopping

If the key value does not exist
# print (my_dict2['gender']) # print (my_dict2['gender']) #
If the key value is not present, no error will be reported, and None will be returned
print(my_dict2.get('gender'))  # none


# my_dict2,get(key, data value) If the key is present, return the corresponding value of the key; if the key is not present, return the written data value
print(my_dict2.get('gender'.'male'))  # male
print(my_dict2.get('age'.1))  # 18
print(len(my_dict2))  # 4.
Copy the code

1.2 Adding and modifying data in the dictionary

my_dict = {'name': 'isaac'}
print(my_dict)  # {'name': 'isaac'}

Add and modify data in the dictionary using the key value
# dictionary [key] = data; If the key value exists, it is modified. If the key value does not exist, it is added

my_dict['age'] = 18   The key value already exists
print(my_dict)  # {'name': 'isaac', 'age': 18}

my_dict['age'] = 19
print(my_dict)  The key value already exists

1 of int and 1.0 of float represent a key
my_dict[1] = 'int'
print(my_dict)   # {'name': 'isaac', 'age': 19, 1: 'int'}
my_dict[1.0] = 'float'
print(my_dict)   # {'name': 'isaac', 'age': 19, 1: 'float'}
Copy the code

1.3 Delete data from the dictionary

my_dict = {'name': 'isaac'.'age': 19.1: 'float'.2: 'aa'}

[key] [key]
del my_dict[1]
print(my_dict)  # {'name': 'isaac', 'age': 19, 2: 'aa'}

# dictionary. Pop (key) delete based on the key value, return the value of the deleted key value
result = my_dict.pop('age')
print(my_dict)  # {'name': 'isaac', 2: 'aa'}
print(result)  # 9


# dictionary.clear () Clears the dictionary and deletes all key-value pairs
my_dict.clear()
print(my_dict)  # {}

The # del dictionary name directly removes the dictionary. It is no longer usable
del my_dict  The following code can no longer use this variable directly unless it is defined again
# print(my_dict) code error, variable is defined
Copy the code

1.4 Dictionary traversal data

my_dict = {'name': 'isaac'.'age': 18.'gender': 'male'}

The body of the for loop iterates directly over the dictionary, iterating over the key of the dictionary
for key in my_dict:
    print(key, my_dict[key])


Keys () retrieves all keys in the dictionary, resulting in type dict_keys, which has the following characteristics
# 1. You can use list () to cast it to a list type
# 2. You can use the for loop for traversal
result = my_dict.keys()
print(result, type(result))  # dict_keys(['name', 'age', 'gender']) <class 'dict_keys'>
for key in result:
    print(key)  # name age gender

Values () retrieves all values of type dict_values
# 1. You can use list () to cast it to a list type
# 2. You can use the for loop for traversal
result = my_dict.values()
print(result, type(result))  # dict_values ([' Isaac, 18, 'male']) < class > 'dict_values'
for value in my_dict.values():
    print(value)  # 18 male Isaac

Items () retrieves all key-value pairs of type dict_item, key, and value as tuples
# 1. You can use list () to cast it to a list type
# 2. You can use the for loop for traversal
result = my_dict.items()
print(result, type(result))
for item in my_dict.items():
    print(item[0], item[1])
print('=' * 30)

for k, v in my_dict.items():
    print(k, v)
Copy the code

1.5 enumerate function

my_list = ['a'.'b'.'c'.'d'.'e']

# for i in my_list:
# print(i) # a b c d e


for i in my_list:
    print(my_list.index(i), i)  # subscript, data value


# enemerate combines the index of an element in an iterable sequence with the specific element data into a tuple
for j in enumerate(my_list):
    print(j)
Copy the code

Conclusion answering questions

Can a list be included in a dictionary?
# Yes, list tuples can store any type of data. Similarly, the dictionary value = can be any type, and the dictionary can also be used as the dictionary value
my_dict = {'name': 'isaac'.'age': 18.'like': [1.2].'aa': {'a': 1.'b': 200}}
print(my_dict)  # {'name': 'isaac', 'age': 18, 'like': [1, 2], 'aa': {'a': 1, 'b': 200}}


print(my_dict['aa'] ['b'])  # 200
my_list = [{}, {}, {}, my_dict]
print(my_list)  # [{}, {}, {}, {'name': 'isaac', 'age': 18, 'like': [1, 2], 'aa': {'a': 1, 'b': 200}}]
Copy the code

Every day to increase a little knowledge, like the Great Wall brick by brick to cover up, thank you for your views, suggestions