Python learning notes (9) Python tuples and dictionaries (2)

Keywords: Python

What is a dictionary

A dictionary is another variable container model and can store any type of object.

Colon is used for every key value = > value pair of the dictionary: divide, comma is used between every key value pair, divide, and the whole dictionary is included in curly bracket {}

The key must be unique, but the value is not.

Value can take any data type, but the key must be immutable, such as string, number or tuple.

Example:

1 >>> d ={"name":"beijing"}  #In curly braces, "name" is key, that is, the key "beijing" is value, that is, the value is divided by colon. Multiple key value pairs are separated by commas, and keys must be unique. Key must be immutable type, value can be any data type
2 >>> d
3 {'name': 'beijing'}
4 >>> type(d)
5 <type 'dict'>
6 >>>

Create dictionary

Method 1: mydict = {}

Method 2: mydict=dict(arg)

Mode 3: mydict={}.fromkeys(arg)

 1 >>> d={}     #Created an empty dictionary
 2 >>> person ={"name":"zhangsan","age":"18"} #Create a dictionary with content
 3 >>> d["student"]="xiaoming"  #Add content to dictionary d
 4 >>> d
 5 {'student': 'xiaoming'}
 6 >>> id(d)      #The id function gets the memory address of the object.
 7 49510328L
 8 >>> d["age"]=18 #Add age to d dictionary
 9 >>> d
10 {'age': 18, 'student': 'xiaoming'}
11 >>> id(d)  #The id function gets the memory address of the object the same as the above. It can be seen that the added content does not create a new dictionary, which means that the dictionary can be modified, or the dictionary can be changed
12 49510328L
13 >>> name=([1,"baidu"],[2,"weibo"]) #Create a tuple. There are two lists in the tuple and two elements in a list. These two elements correspond to the key and value to be created
14 >>> name
15 ([1, 'baidu'], [2, 'weibo'])
16 >>> web =dict(name) #Using the dict function to create a dictionary
17 >>> web
18 {1: 'baidu', 2: 'weibo'}
19 >>> w =dict(name="tom",age=10) #Using the dict function to create a dictionary
20 >>> w
21 {'age': 10, 'name': 'tom'}
22 >>> book ={}.fromkeys(("python","author"),"cc") #Using fromkeys to create a dictionary, you can see that the value value can be repeated
23 >>> book
24 {'python': 'cc', 'author': 'cc'}
25 >>>

Basic method

len() key value pair length

d[key] get value value through key

d[key]=value modify or add

del[key] delete

key in d judge whether key is in dictionary d

 1 >>> city_codes
 2 {'tianjin': '022', 'beijing': '010', 'shanghai': '021', 'chongqing': '023'}
 3 >>> "beijing is the capital of China ,its area code is %(beijing)s" % city_codes
 4 'beijing is the capital of China ,its area code is 010'
 5 >>> len(city_codes) #
 6 4
 7 >>> city_codes["beijing"]
 8 '010'
 9 >>> city_codes["nanjing"]="025"
10 >>> city_codes
11 {'tianjin': '022', 'beijing': '010', 'shanghai': '021', 'nanjing': '025', 'chongqing': '023'}
12 >>> del city_codes["shanghai"]
13 >>> "shanghai" in city_codes
14 False
15 >>>

Key value pairs: mapping relationships

1 >>> city_codes={"beijing":"010","shanghai":"021","tianjin":"022","chongqing":"023"}
2 >>> city_codes["beijing"] #Access the dictionary value through the key of the dictionary
3 '010'
4 >>> city_codes   #The dictionary is out of order
5 {'tianjin': '022', 'beijing': '010', 'shanghai': '021', 'chongqing': '023'}
6 >>> "beijing is the capital of China ,its area code is %(beijing)s" % city_codes  #You can use a dictionary for formatting strings (note)
7 'beijing is the capital of China ,its area code is 010'
8 >>>                                                                                                

Posted by felipeebs on Thu, 02 Apr 2020 19:04:11 -0700