Inserting or adding a dictionary to a dictionary
First we need to understand how the dictionary is written
dictionary name = { 'linchpin1(key1)':'(be) worth1(value1)', 'linchpin2(key2)':'(be) worth2(value2)', ...... }
Now there are dictionaries dict_1 = {}, dictionary dict_2 = {'name': 'Lihua', 'age': 19} , dict_3 = {'name': 'Xiaohong', 'age': 18}
We want to add the dictionary dict_2 and the dictionary dict_3 to dict_1
We could write it like this.
dict_2 = {'name': 'Lihua', 'age': 19} dict_3 = {'name': 'Little Red', 'age': 18} dict_1 = {} dict_1['key_1'] = dict_2 dict_1['key_2'] = dict_3 print(dict_1) # Output #{ # 'key_1': {'name': 'Lihua', 'age': 19}, # 'key_2': {'name': 'Little Red', 'age': 18} #}
The name of the key can be written as you wish.
Or you can write a loop that uses numbers as keys
info_list = [{'name': 'Lihua', 'age': 19}, {'name': 'Little Red', 'age': 18}] dict_1 = {} for i in range(2): dict_1[i] = info_list[i] print(dict_1) # Output #{ # 0: {'name': 'Lihua', 'age': 19}, # 1: {'name': 'Little Red', 'age': 18} #}
Why write this way, this is because I am crawling Ajax content to get the data in json format, the use of the above method to parse the data is conducive to write the data in json format into the exel
Or you can use update() to add key-value pairs to the dictionary
dict_2 = {'name': 'Lihua', 'age': 19} dict_3 = {'name': 'Little Red', 'age': 18} dict_4 = {'habit': 'sport', 'high': 178} dict_1 = {} dict_1.update(dict_2) print(dict_1) # Output {'name': 'Lihua', 'age': 19} dict_2.update(dict_3) # The contents of dict_2 are overwritten because the two dictionary keys are the same print(dict_2) # Output {'name': 'red', 'age': 18} dict_3.update(dict_4) print(dict_3) # exports {'name': 'Little Red', 'age': 18, 'habit': 'sport', 'high': 178}
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.