Introduction to dict ionary

Posted by nexgen_x on Mon, 07 Mar 2022 17:00:19 +0100

1, Application scenario: we learned previously that data types such as lists and tuples can store multiple data. Although multiple data can be stored, once the data location changes, does the data match. For example: [Xiao Wang, male, Xiao Hong, female] if the position of male and female changes, does the data match. Therefore, this kind of data that needs one-to-one correspondence can be stored in a dictionary.

Function: 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

2, Introduction and use of dictionary

No matter how the dictionary is used to find the data, it only needs to describe the data in the form of the corresponding key and the corresponding value in the dictionary. That is, it only needs to search the data in the form of the data in the later stage, no matter how the corresponding key and value appear in the dictionary

Brace is {1}

2. The data appears in the form of key value pairs

3. Key value pairs are separated by colons

use:

Format: variable name = {key: value, key: value} # dictionary has data creation format

Variable name = {} # empty dictionary creation format

Variable name = dict() # empty dictionary creation format

dic1 = {"name":"Ah, Jay","age":18}   # Data creation format
dic2 = {}   # Empty dictionary creation format
dic3 = dict()
print(dic1)
print(type(dic1))   # Type view data type
print(dic2)
print(type(dic2))   # Type view data type
print(dic3)
print(type(dic3))   # Type view data type

Operation results:

 

III. methods of obtaining data in the dictionary:

There are two ways:

The first one: if there is the data you are looking for in the dictionary, it will return the value, and if there is no data, it will report an error.

Format: variable name = {key: value}

Print (variable name. [key])

# Get the value in the dictionary
dic1 = {"name":"Ah, Jay","age":18}   # Data creation format
print(dic1["name"])     # The key exists in the dictionary, so it returns a value
print(dic1["height"])   # This key is not in the dictionary, so an error will be reported

Operation results:

The second method: if the data does not exist, there will be no error in the dictionary.

get(): method

Format: variable name = {key: value}

Print (variable name. get("key") # without this data, a None will be returned

Print (variable name. get("key", "value") # this will return the following value

dic1 = {"name":"Ah, Jay","age":18}   # Data creation format
print(dic1.get("height"))   # Return a None
dic2 = dic1.get("height",166)
print(dic2) # Returns a value (166)

Operation results:

4, Dictionary addition, deletion and reference

1. Dictionary - add

Format: variable name = {key: value}

Format: variable name [key] = value

dic1 = {"name":"Ah, Jay","age":18}
dic1["height"] = 168    # Variable name [key] = value
print(dic1)

Operation results:

2. Dictionary --- delete

del has two methods: the first is to delete the data in the dictionary; The second is to delete the entire dictionary.

Format: del dictionary variable name # this is to delete the entire dictionary

Format: del dictionary variable name ["key to delete"]

dic1 = {"name":"Ah, Jay","age":18}
del dic1["name"]    # Delete "name" from the dictionary
print(dic1)    # Only the specified data in the dictionary will be deleted
del dic1    # Delete entire dictionary
print(dic1)    # After deleting the dictionary, an error will be reported in printing

Operation results:

 3. Dictionary - look up

keys(): find all the key s in the dictionary

Format: Dictionary variable name keys()

values(): find all value s in the dictionary

Format: Dictionary variable name values()

items(): find all key value pairs of the dictionary

Format: Dictionary variable name items()

dic1 = {"name":"Ah, Jay","age":18}
print(dic1.keys())  # Find all keys in the dictionary
print(dic1.values())  # Find all values in the dictionary
print(dic1.items())  # Find all key value pairs in the dictionary

Operation results:

5, Dictionary key, value and key value pair traversal

1. Key to traverse the dictionary:

dic1 = {"name":"Ah, Jay","age":18}
for i in dic1.keys():
    print(i)

2. Traverse dictionary values:

dic1 = {"name":"Ah, Jay","age":18}
for i in dic1.values():
    print(i)

3. Traverse the key value pairs of the dictionary:

dic1 = {"name":"Ah, Jay","age":18}
for i in dic1.items():
    print(i)

The three traversal run results are put together:

6, Enumeration

The enumerate() function is used to combine a traversable data object into an index sequence, and list the data and data subscripts at the same time. It is generally used in the for loop.

Format: enumerate (iteratable object, subscript)

1. Dictionary example:

dic1 = {"name":"Ah, Jay","age":18} 
mj = enumerate(dic1,0)  # The default output key and key subscript can also be used with keys (), values (), and items ()
for m,j in mj:  # m receives the key of dic1 and j receives the subscript of the key
    print(m,j)

Operation results:

 2. List example:

li1 = [1,2,3,4,5,6]
mj = enumerate(li1,0)
for m,j in mj:
    print("Subscript:",m,"Value:",j)

Operation results:

Topics: Python