python basis of three (dictionary)

Dictionary:
 access where each value by a data structure called mapping names, the dictionary is built in python mapping types, which are arranged in order of Value. Key may be a number, a string, a tuple.

1.1, to create and use a dictionary
dictionary similar to the following manner, said:
python basis of three (dictionary)
# dictionary of keys and values that make up this key: value called items. As the name and age and sex is jzh, 18, and M, and an intermediate key value in the colon split the key figure above, using outside enclosed in braces.

1.2, dict function
for the key sequence is converted into the dictionary or created Dictionary
python basis of three (dictionary)

python basis of three (dictionary)

1.3, the basic operation of the dictionary

len(d)                #返回字典的项
d[k]                   #返回键对应的值
d[k]=values      #将值赋给值
del d[k]             #删除健为k的项
k in d                #检查字典d是否包含健为k的项

python basis of three (dictionary)

python basis of three (dictionary)

python basis of three (dictionary)

python basis of three (dictionary)

python basis of three (dictionary)


Tip:
type key : dictionary can be a key number, it can not be an integer.
Automatically add : even if the value is not in the dictionary, you can assign to it.
Membership : relative to check whether the list contains the appropriate value, check whether the dictionary contains the specified key to higher efficiency.


1.4, the dictionary method
clear method (Empty dictionary)
python basis of three (dictionary)

copy method (returns a new dictionary, this method is a shallow copy )
python basis of three (dictionary)

注:假如copy的字典理由列表,如果修改了复制的字典里列表的值,那么原来的字典值也将会改变,可以使用copy模块中的deepcopy来执行深复制

fromkeys process (creating a new dictionary contains the specified key into, and each key corresponds to a value of None)
python basis of three (dictionary)

python basis of three (dictionary)

get method (if there is no access to a key throws an exception, get it will not)
python basis of three (dictionary)

python basis of three (dictionary)

items (return all items)
python basis of three (dictionary)

keys (to return all keys)
python basis of three (dictionary)

pop (delete the specified items)
python basis of three (dictionary)

popitem (pop a random element)
python basis of three (dictionary)

setDefault (similar to get, but in the dictionary does not contain this value, add the specified key-value pairs in a dictionary)
python basis of three (dictionary)

values ​​(the return value of the dictionary)
python basis of three (dictionary)

Guess you like

Origin blog.51cto.com/12020040/2427749