Dictionary dict:
A dictionary is actually the equivalent of a map in Java to store key-value pairs. The data stored in it is unordered.
If there is such a data:
t1=[' name ', ' age ', ' sex '
t2=[' Tom ', ' Male '
by Zip (t1,t2) you can get to such data [(' Name ', ' Tom '), (' Age ', ' + '), (' Sex ', ' male ')]
The dictionary definition is done through {}.
A dictionary is the only type of mapping (hash table) in Python, where the Field object is variable, but the key of the Dictionary object must be immutable, and a dictionary can contain key values of the type.
Use keys () or values () to return the dictionary's key list or value list.
Items () returns a tuple of key-value pairs.
Dictionary Creation Method:
1. {}
For example d={' name ': ' Tom ', ' age ': +, ' sex ': ' Male '}
2, Factory method Dict ()
D = dict ()
D = dict (name= "Nico", age=23)
D = dict ([' Name ', "Nico"], [' Age ', 23])
3, built-in Method Fromkeys (), this comparison is suitable for the dictionary keys have the same value.
Ddict={}.fromkeys ((' x ', ' Y '),-1)
get {' x ':-1, ' Y ':-1}
Iterate through a dictionary
1d={'name':'Tom',' Age': 29,'Sex':'male'}2 3 forKinchD:4 Printk5 6 It's the same as this print. 7 8 forKinchD.keys ():9 PrintK
How to do the dictionary:
1. New
d[' Tel ']= ' 1234568 '
At this point D is {' name ': ' Tom ', ' age ': ' Sex ': ' Male ', ' tel ': ' 12345678 '}
2. Modification
d[' Tel ']= ' 88888 '
3. Delete
Del (d[' tel ') Delete one of the key values in the dictionary
Del d Delete the entire dictionary
D.pop (' tel ') removes and returns an element with a key value of Tel
D.clear () empties the data in the entire dictionary
4. Take value
d[' name ']
Output Tom
If you take a nonexistent key value and report a Keyerroe exception, Python provides the D.get () method
such as D.get (' address ') is not output, you can also specify the default return value when not: D.get (' Address ', ' ERROR ') output error
Dictionary-related functions:
Type () CMP () str ()
http://www.icoolxue.com/play/1924 Nineth Episode
Python Learning (11) Python Data type: Dictionary (important)