day07_ Dictionary of data sequences

Posted by terrabull on Thu, 28 Oct 2021 06:23:27 +0200

What is a dictionary?

A dictionary is a container. The data in the dictionary appears in the form of key value pairs. The dictionary data has nothing to do with the data order, that is, the dictionary does not support subscripts. No matter how the data changes in the later stage, you only need to find the data according to the name of the corresponding key.

Dictionary features:

  • The symbol is braces
  • Data appears in the form of key value pairs, which are generally called keys in front of colons, or k for short; The value following the colon is v for short.
  • Key value pairs are separated by commas
  • The dictionary is of variable type.

Code example

# There is a data dictionary
dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}

# Empty dictionary mode 1:
dict2 = {}
# Empty dictionary mode 2:
dict3 = dict()

Addition, deletion, modification and query of dictionary elements

Dictionary adding and modifying elements

If the key exists, modify the value corresponding to the key; If the key does not exist, add this key value pair.

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}

dict1['name'] = 'Rose'
print(dict1)  # {'name': 'Rose', 'age': 20, 'gender': 'male'}

dict1['id'] = 110
print(dict1)  # {'name': 'Rose', 'age': 20, 'gender': 'male', 'id': 110}

Dictionary delete element

  • Del() / del: delete the dictionary or delete the specified key value pair in the dictionary.
  • clear(): clear the dictionary
dict1 = {'name': 'TOM', 'age': 20, 'gender': 'male'}

del (dict1)  # del deletes the dictionary container

del dict1['name']  # Delete the specified element through the key
del dict1['names']  # Delete the specified element through the key. It may not exist and an error is reported

# clear() clears the elements in the dictionary and keeps the empty collection
dict1.clear()

Dictionary lookup element

  • Key value lookup: if the currently searched key exists, the corresponding value will be returned; Otherwise, an error is reported.

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
print(dict1['name'])  # Tom
print(dict1['id'])  # report errors
  • get() function: returns the corresponding value if the key currently searched exists. If the key currently searched does not exist, the second parameter (default value) is returned. If the second parameter is omitted, None is returned.

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
print(dict1.get('name'))  # Tom
print(dict1.get('names'))  # None
print(dict1.get('names', 'Lily'))  # Lily
  • keys() function: find all key s in the dictionary and return iteratable objects

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
print(dict1.keys())  # dict_keys(['name', 'age', 'gender'])
  • values() function: find all value s in the dictionary and return iteratable objects
dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
print(dict1.values())  # dict_values(['Tom', 20,' male '])

  • items() function: find all key value pairs in the dictionary and return iteratable objects. The data inside is tuple, tuple data 1 is the key of the dictionary, and tuple data 2 is the value corresponding to the key of the dictionary

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
print(dict1.items())  # dict_ Items ([('name ',' Tom '), ('age', 20), ('gender ',' male ')])

Circular traversal of dictionary

key traversing dictionary

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
for i in dict1.keys():
    print(i)

Traverse the value of the dictionary

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
for i in dict1.values():
    print(i)

Traversing dictionary elements

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
for i in dict1.items():
    print(i)

Traverse the key value pairs of the dictionary

dict1 = {'name': 'Tom', 'age': 20, 'gender': 'male'}
for k, v in dict1.items():
    print(f"{k} corresponding:{v}")

Topics: Python