Dictionaries and collections
Dictionary base Operations
Create a dictionary
Create a dictionary with the {} operator
Create a dictionary from the Dict () factory function
Create a default dictionary with the same value by Fromkeys ()
>>> adict = {' Name ': ' Bob ', ' age ': 23}
>>> Import Tab
>>> bdict = dict ([' name ', ' Bob '],[' age ', 23])
>>> Print Bdict
{' age ': $, ' name ': ' Bob '}
>>> cdict = {}.fromkeys (' Bob ', ' Alice '), 23)
>>> Print Cdict
{' Bob ': all, ' Alice ': 23}
>>> dict ([' AB ', ' CD ', ' EF '])
{' a ': ' b ', ' C ': ' d ', ' e ': ' F '}
>>> dict ([' name ', ' Bob '), (' age ', ' + '), [' qq ', ' 123455 ']])
{' QQ ': ' 123455 ', ' age ': ' name ': ' Bob '}
Accessing dictionaries
A dictionary is a mapping type, meaning that it has no subscript, and that accessing the values in the dictionary requires the use of the corresponding key
>>> adict
{' age ': ' name ': ' Bob '}
>>> for Eachkey in Adict:
... print "key =%s,value =%s"% (eachkey,adict[eachkey])
...
Key = Age,value = 20
Key = Name,value = Bob
>>> print "% (name) s"%adict
Bob
Update dictionary
Updating dictionaries with keys
-update related values if the key is in the dictionary
-adds a new value to the dictionary if the key is not in the dictionary
>>> Print Adict
{' age ': ' name ': ' Bob '}
>>> adict[' Age '] = 22
>>> Print Adict
{' age ': $, ' name ': ' Bob '}
>>> adict[' Email ']= ' [email protected] '
>>> Print Adict
{' age ': ' name ': ' Bob ', ' email ': ' [email protected] '
Delete Dictionary
You can delete an element in a dictionary or an entire dictionary by using Del
Use the internal method clear () to empty the dictionary
Use the pop () method to "eject" the elements in the dictionary
>>> Print Adict
{' age ': ' name ': ' Bob ', ' email ': ' [email protected] '
>>> del adict[' Email ']
>>> Print Adict
{' age ': $, ' name ': ' Bob '}
>>> Adict.pop (' Age ')
22
>>> Print Adict
{' name ': ' Bob '}
>>> Adict.clear ()
>>> Print Adict
{}
>>> adict = {' name ': ' Bob ', ' age ': '
' >>> adict
{' age ': ' name ': ' Bob '}
>>> Adict.popitem () #随机pop出一个键值, not commonly used, because it does not accept Parameters.
(' Age ', 20)
>> > Adict
{ ' Name ': ' Bob '}
>>> Adict.popitem (' Bob ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:popitem () takes no arguments (1 given)
Dictionary operators
Use the dictionary key to find the operator [keyname] to find the value corresponding to the key
Use in and not to determine if the key exists in the dictionary
>>> adict = {' name ': ' Bob ', ' age ': 20}
>>> Print adict[' name ']
Bob
>>> adict
{' age ': ' name ': ' Bob '}
>>> ' Bob ' in Adict
False
>>> ' name ' in Adict
True
dictionary-related functions
Functions that act on a dictionary
Len (): returns the number of elements in the dictionary
Hash (): It is not designed for dictionaries, but can be used to determine whether an object can be a key to a dictionary
>>> Print Adict
{' age ': ' name ': ' Bob '}
>>> print len (adict)
2
>>> Hash (3) # No error description can be used as a dictionary key, error cannot be used as a dictionary key
3
>>> Hash ([]) #总结: immutable type can be used as a dictionary key, variable not
Traceback (most recent):
File "<stdin>", line 1, in <module>
typeerror:unhashable type: ' list '
>>> Hash (' name ')
15034981
>>> Hash ((10,20))
-95038731
Dictionary built-in Methods
Dict.copy (): Returns a copy of the dictionary (shallow copy (copy value only, ID different))
>>> Print Adict
{' age ': ' name ': ' Bob '}
>>> bdict = adict.copy ()
>>> bdict
{' age ': ' name ': ' Bob '}
>>> bdict[' Name '] = ' Alice '
>>> Print Bdict
{' age ': ' name ': ' Alice '}
####################
>>> adict
{' age ': ' name ': ' Bob '}
>>> bdict = adict
>>> bdict[' Age '] = 22
>>> bdict
{' age ': $, ' name ': ' Bob '}
>>> adict
{' age ': $, ' name ': ' Bob '}
Dict.get (key,default=none): Returns the value of the key in the dictionary dict, if the key is not present in the dictionary, and returns the value of default
>>> Print Adict
{' age ': ' name ': ' Bob '}
>>> adict.get (' name ', ' not found ')
' Bob '
>>> Adict.get (' Email ', ' Not found ')
' not found '
Dict.items (): Returns a list containing the dictionary (key, Value) to the tuple
Dict.keys (): Returns a list containing the keys in the dictionary
>>> Adict.keys ()
[' QQ ', ' age ', ' name ']
Dict.values (): Returns a list that contains all the values in the dictionary
>>> adict.values ()
[' 2222333 ', ' Bob ']
Dict.update (dict2): adds a dictionary dict2 key-value pair to the dictionary Dict
>>> adict.update ({' phone ': ' 12345678901 ', ' Emali ': ' [email protected] '})
>>> adict
{' QQ ': ' 2222333 ', ' phone ': ' 12345678901 ', ' age ': ' name ': ' Bob ', ' Emali ': ' [email protected] '
Dict.setdefault (): Security adds key-value to the dictionary, key-value that already exist in the dictionary do not overwrite
>>> adict
{' age ': $, ' name ': ' Bob '}
>>> Adict.setdefault (' Age ', 30)
22
>>> adict
{' age ': $, ' name ': ' Bob '}
>>> adict.setdefault (' qq ', ' 2222333 ')
' 2222333 '
>>> adict
{' QQ ': ' 2222333 ', ' age ': ' name ': ' Bob '}
Collection
Collection type
Collection type operator
Set built-in methods
This article is from the Linux server blog, so be sure to keep this source http://sailq21.blog.51cto.com/6111337/1859562
Python Learning notes Day05-python dictionaries and collections, conditions and loops, files, and input and output