This is the second day of my participation in Gwen Challenge

Author: Cola

Source: Coke’s path to data analysis

Please contact authorization for reprinting (wechat ID: data_COLA)

Writing in the front

Another data structure, the dictionary, is coming

A dict, “dict,” is a key-value pair of data structures enclosed in curly braces {}. Dictionaries are usually represented like this:

Dictionary name = {key1:value1,key2,value2}

Key is a key, value is a value, a set of keys and values are separated by colons, and different key-value pairs are separated by commas. Keys can be any data type, and values can be any data type including data structures, such as lists. You can use the type function to view the data type of a dictionary as dict

In [1] : # new dictionary t_dict = {" Tony ": 101," Tom: "102," Judy ": 103," baby ": 104," Cindy ": 105} type (t_dict) Out [1] : dictCopy the code

1, build

Similarly, we learn from the perspectives of build, add, delete, check and change. Create a new dictionary and pass in the key-value pair structure. The keys are characters and the values are values:

In [2] : # new dictionary t_dict = {" Tony ": 101," Tom: "102," Judy ": 103," baby ": 104," Cindy ": 105} t_dict Out [2] : {' Tony ': 101,' Tom ': 102, 'judy': 103, 'baby': 104, 'cindy': 105}Copy the code

You can also pass in lists to create dictionaries

In [3] : # list to create the dictionary l_dict = {" name ": [" Tony", "Tom", "Judy", "Cindy"], "num" : [101102103104]} l_dict Out [3] : {" name ": ['tony', 'tom', 'judy', 'cindy'], 'num': [101, 102, 103, 104]}Copy the code

2,

Add key-value pairs to dictionaries, for example, add age key-value pairs to l_dict dictionaries.

In [4] : # new keys to l_dict [" age "] = [14,15,16,17] l_dict Out [4] : {' name ': [' Tony', 'Tom', 'Judy', 'Cindy'], "num" : [101, 102, 103, 104], 'age': [14, 15, 16, 17]}Copy the code

3, delete

To delete key-value pairs, use the del function to delete them by key

In [5] : # delete keys to del l_dict l_dict Out [5] [" age "] : {' name ': [' Tony', 'Tom', 'Judy', 'Cindy'], 'num: [101, 102, 103, 104].Copy the code

4,

4.1 Whether the lookup key is in the dictionary

Using the in operator, the keys method is used to retrieve keys in the dictionary and find out if age is a key in l_dict

In [6]:# search value 105 In l_dict.values() Out[6]:FalseCopy the code

4.2 Finding whether the value is in the dictionary

The Values method is used to get the Values in the dictionary

In [7]:# find key "age" In l_dict.keys() Out[7]:TrueCopy the code

4.3 Finding the specified value in the Dictionary

Find the corresponding value by key and access it directly by key:

In [8] : # consult value according to the key l_dict [" name "] Out [8] : [' Tony ', 'Tom', 'Judy', 'Cindy']Copy the code

The value, key, and item keys methods are used to get all the keys in the dictionary, the values method is used to get all the values, and the items method is used to get the entire dictionary

In [9] : # for all key l_dict. The keys () Out [9] : dict_keys ([' name ', 'num']) In [10] : # for all the values of the l_dict. The values () Out [10] : the dict_values ([[' Tony ', 'Tom', 'Judy', 'Cindy'], [101, 102, 103, # 104]] In [11] : get the entire dictionary l_dict. The items () Out [11] : dict_items ([(' name '[' Tony', 'Tom', 'Judy', 'Cindy']), (" num ", [101, 102, 103, 104]])Copy the code

5, change

Just like a list, search and assign. For example, change the num key 104 to 109 in l_dict.

In [12] : # modified value l_dict [" num "] = [101102103109] l_dict Out [12] : {' name ': [' Tony', 'Tom', 'Judy', 'Cindy'], "num" : [101, 102, 103, 109], 'age': [14, 15, 16, 17]}Copy the code

conclusion

Create a list: curly braces {key: value} keys get keys values get values items get items