I. Dictionary Help documentation
>>> dir (dict) [' __class__ ', ' __cmp__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __doc__ ', ' __eq__ ', ' __ Format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __iter__ ', ' __le__ ', ' __len_ _ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __setitem__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' clear ', ' copy ', ' Fromkeys ', ' get ', ' has_key ', ' Items ', ' iteritems ', ' Iterkeys ', ' Iterv ' Alues ', ' Keys ', ' Pop ', ' Popitem ', ' setdefault ', ' Update ', ' values '
Second, the characteristics of the dictionary
1, the dictionary is unordered, it can not be accessed by the offset, only through the key to access. can be nested, dictionary = {' key ': value} key: A key similar to our reality, and value is a lock. A key to open a lock
>>> info={' A ': 1, ' B ':2}>>> info{' a ': 1, ' B ': 2}>>> info[' a ']1
2, the dictionary internal no order, through the key to read the content, can be nested, convenient for us to organize a variety of data structures, and can modify the contents in situ, belong to the mutable type.
>>> binfo = {' A ': [3], ' B ':[4,5,6]}>>> binfo{' a ': [1, 2,], ' B ': [4, 5, 6]}>>> binfo[' a '][2] =5>>> binfo{' A ': [1, 2, 5], ' B ': [4, 5, 6]}
3. The keys that make up the dictionary must be immutable data types, such as numbers, strings, tuples, and so on, and mutable objects such as lists cannot be keys.
>>> binfo={1: ' $ ', 2: ' DD '}>>> binfo{1: ' 2: ' dd '}>>> cinfo={': ' 222 ', ' AA ':11}> >> cinfo{' AA ': One, ' $ ': ' 222 '}>>> dinfo={(All-in-one): ' SS ', (' B ', ' C '): ' 222 '}>>> dinfo{(' B ', ' C '): ' 222 ', (1, 2, 3): ' SS '}
The elements inside the tuple must also not be changed.
>>> dinfo={(1,2,[1,3]): ' SS ', (' B ', ' C '): ' 222 '}traceback (most recent call last): File "<stdin>", line 1, in ? Typeerror:list Objects is unhashable
Iii. Common operations of dictionaries
1. Create a dictionary. {},dict ()
info = {' name ': ' Lilei ', ' age ': 20}>>>-info{' age ': ' Name ': ' lilei '}info = dict (name= ' Lilei ', age=20) >>& Gt info{' age ': ' Name ': ' Lilei '}
2, add content a[' xx '] = ' xx ', key does not exist, that is, to add
info[' phone ' = ' iphone5 ' >>> info{' phone ': ' iphone5 ', ' age ': ' Name ': ' Lilei '
3, modify the content a[' xx '] = ' xx ', key exists, that is, to modify
info[' phone ' = ' HTC ' >>> info{' phone ': ' HTC ', ' Age ': ' ' name ': ' Lilei '}
The update parameter is a dictionary type that overrides the value of the same key
Info.update ({' City ': ' Beijing ', ' phone ': ' Nokia '}) >>> info{' phone ': ' Nokia ', ' Age ': ' ' name ': ' Lilei ', ' City ' : ' Beijing ' #htc turned into Nokia.
4. Delete Del,clear,pop
Del info[' phone '] #删除某个元素
>>> info = {' name ': ' Lilei ', ' age ': 20}>>>-info.update ({' City ': ' Beijing ', ' phone ': ' Nokia '}) >> > info{' city ': ' Beijing ', ' age ': +, ' name ': ' Lilei ', ' phone ': ' Nokia '}>>> del info[' City ']>>> info{ ' Age ': ' Name ': ' Lilei ', ' phone ': ' Nokia '}
>>> del info[' city '] #删除已经不存在的key, will error Traceback (most recent): File "<stdin>", line 1, in <mo Dule>keyerror: ' City '
Info.clear () Delete all elements of a dictionary
>>> info.clear () >>> info{}
Pop deletes the specified key
>>> info = {' name ': ' Lilei ', ' age ': 20}>>>-info{' age ': ' Name ': ' Lilei '}
>>> info.pop (' name ') #删除key为name的元素 and returns key corresponding to value ' Lilei ' >>> info{' age ': 20}
>>> info.pop (' name ') #删除已经不存在的key, will error Traceback (most recent call last): File "<stdin>", line 1, in <mo Dule>keyerror: ' Name '
>>> info.pop (' name ', ' Defalutvalue ') #key不存在, returns the specified value ' defalutvalue ' defalutvalue ' >>> info{' age ': 20 }
5, the Dictionary pop method and the list of pop method differences, the dictionary pop delete the nonexistent key, you can specify the default value
>>> l=[1,2,3,4]>>> L.pop () #不指定参数, is the last element of pop 4>>> l[1, 2, 3]>>> L.pop (2) # You can specify which element to delete 3>>> L.pop (2) #下标不存在的元素会报错Traceback (most recent call last): File "<stdin>", line 1, in <mo Dule>indexerror:pop index out of range
6, in and Has_key () member relations operations
>>> info = {' name ': ' Lilei ', ' age ': 20}>>>-info{' age ': ' "Name ': ' Lilei '}>>> ' name ' in Infotru E>>> info.has_key (' name ') True
7. Keys (): Returns a list containing all the keys of the dictionary
VALUES (): Returns a list containing all the values of the dictionary
Items: Generating a dictionary container: [()]
>>> info = {' name ': ' Lilei ', ' age ': 20}>>> Key=info.keys () >>> key[' age ', ' name ']>>> Values=info.values () >>> values[20, ' Lilei '
>>> Info.items [(' Age ', ' + '), (' Name ', ' Lilei ')]
8. Get: Get a value from the dictionary
>>> info = {' name ': ' Lilei ', ' age ': 20}>>>-info{' age ': ' Name ': ' Lilei '}>>> info.get (' name ' ) ' Lilei '
>>> b=info.get (' Age21 ') #如果是不存在的key, return nonetype>>> type (b) <type ' Nonetype ' >
>>> info.get (' Age2 ', ' a ') #如果是不存在的key, can be specified to return a default value of ' 22 '
Practice:
Known dictionaries: Ainfo = {' ab ': ' liming ', ' AC ': 20}
Complete the following actions
1 using 2 methods, the result of the output:
Ainfo = {' ab ': ' liming ', ' ac ': ' Sex ': ' Man ', ' Age ': 20}
>>> ainfo = {' ab ': ' liming ', ' AC ': 20}
>>> ainfo[' sex ']= ' man '
>>> ainfo[' age ']=20
>>> Ainfo
{' AC ': ' AB ': ' liming ', ' age ': ' + ' sex ': ' Man '}
>>> ainfo.update ({' Sex ': ' Man ', ' Age ': 20})
>>> Ainfo
{' AC ': ' AB ': ' liming ', ' age ': ' + ' sex ': ' Man '}
2 output result: [' ab ', ' AC ']
>>> Ainfo.keys () [0:2]
[' AC ', ' AB ']
3 output result: [' liming ', 20]
4 returns the value of the key name AB by 2 methods.
5 The value of the AC corresponding to the key name is removed by 2 methods.
This article from "Small Fish Blog" blog, declined reprint!
Features and common operations of the Python dictionary