Dictionary definitions and attribute dictionaries are the only types of mappings in the Python language.
definition:{key1:value1,key2:value2}
1、键与值用冒号“:”分开;2、项与项用逗号“,”分开;
Characteristics:
1.key-value结构2.key必须可hash、且必须为不可变数据类型、必须唯一3.可存放任意多个值、可修改、可以不唯一4.无序
To Create a dictionary:
person
=
{
"name"
:
"mr.wu"
,
‘age‘
:
18
}
或
person
=
dict
({
"name"
:
"mr.wu"
,
‘age‘
:
18
})
Common operations:
- Index
- New
1 Dic_t[ " k2 " ] = Span style= "COLOR: #800000" > " v2 2 Dict.fromkeys ([ " key1 ", " key2 ", " key3 "], " value " # loop key list, generate a dictionary, if the following values are not assigned, all are none, if there is an assignment, the value of key is all for this string
- Delete
1 Dic_t.pop ("key") # Deletes the key-value pair and returns the corresponding value2 dic_ T.popitem () # randomly delete 3 del dic_t["key "]
- Modify
1 dic_t.update (v2) # Merge two dictionaries, if the same key,list2 overrides the list key
- Enquiry
1"Key"inchdic_t#Standard usage2Dic_t.get ("Key")#get Value,key does not exist then return none3dic_t["key] #获取value, key does not exist then error4Dic_t.items ()#convert key and values into a small tuple5Dic_t.setdefault ("Key","Values")#If the list has this key, it gets the value of the key, and if not, creates a key-value pair
- Key, value, key-value pairs
1 Dic_t.keys () # print all the keys2 dic_t.value () # Print all the values 3 Dic_t.items () # Print all key-value pairs
- Cycle
for key in dic_t: #循环key
For key,values in Dic_t.items (): #循环键值对
- Length
enumrate ()Add an ordinal to an object that can be iterated 1 li = [11,22,33] 2 for k,v in Enumerate (LI, 1): 3 print(k,v)
Getting started with Python-understanding