Today, learning the use of dictionaries, so-called dictionary is actually key value pairs of data, a dictionary has a unique key corresponding to a value,key is unique, value is not unique. Adding the same key in. NET will cause an error, in Python, if the same key is present, the value of the following key will overwrite the previous data.
I. Basic usage of dictionaries.
1. Dictionary creation: the creation format of the field is {Key:value,key:value}, or it can be converted by the Dict function.
The order of Note:key is sorted by string from small to large, and if the dictionary is declared with the same key, take the last one. (different from. NET)
>>> dic1={'Name':'Hardy',' Age': -}>>>dic1{' Age': -,'Name':'Hardy'}>>> imtes=[('Name','Frank'),(' Age', at)]>>> dic =dict (imtes);>>>dic{' Age': at,'Name':'Frank}
>>> dic2={' name ': ' Hardy ', ' age ': ' Name ': ' Frank '} #第一个被覆盖了
>>> Dic2
{' Age ': +, ' Name ': ' Frank '}
2. Basic use of dictionaries
>>> imtes=[('Name','Frank'),(' Age', at)]>>> dic =dict (imtes);>>>dic{' Age': at,'Name':'Frank'}>>>Len (DIC) #长度2>>>' Age' inchDiC #判断是否有Key存在True>>> dic[' Age'] = A#修改值>>>dic{' Age': A,'Name':'Frank'} >>> del dic[' Age'] #删除某个元素>>>dic{'Name':'Frank'}
3. Formatting of the dictionary.
The preceding totals will be two formats, one for formatting data through a tuple, and one for using a dictionary format for data through template templates. The following explains the dictionary formatting data.
>>> workmsg = {'Frank':'Coder','Nancy':'HR','Vincent':'Project Manager'}>>>'Frank is the% (Frank) s'%workmsg'Frank is the coder'
two. The use of methods is provided within the dictionary .
1.Clear method: When using the Clear method, let's look at the following three examples.
Note: The surface through dic={} is to erase the data.
First case: DicTestB1 and dicTestA1 all point to the same space. But for dictesta1={} operation, actually opened a new space, DicTest1 point to the corresponding space {}, so dicTestB1 still retain the original data.
A second case: DicTestB2 and DicTestA1 point to the same space. But Dictesta1.clear () is empty for the current space and does not produce any new space, so dicTestB2 has no data.
The third case: since the copy () method was called, the new space was opened at Dictestb3=dictesta3.copy (), so dicTestB3 and dicTestA3 were irrelevant. So dicTestA3 any action does not affect the DicTextB3 value
>>> dicTestA1 = {'Name':'Frank'}>>> dicTestB1 =dicTestA1>>> dictest1={}>>>dictest1{}>>>dictestb1{'Name':'Frank'}>>> >>> dictesta2={'Name':'Frank'}>>> dictestb2=dicTestA2>>>dictesta2.clear ()>>>dictestb2{}>>> >>> dictesta3={'Name':'Frank'}>>> dictestb3=dicTestA3>>> dicTestB3 =dictesta3.copy ()>>>dictesta3.clear ()>>> dictesta3={}>>>dictestb3{'Name':'Frank'}
2.fromkeys: Add a key with the values null for the dictionary.
>>> {}.fromkeys (['Name','age']) {' Age'Name': None}}
>>> {}.fromkeys ([' Name ', ' age '], ' unkown ')
{' Age ': ' unkown ', ' Name ': ' Unkown '}
3. Has_key () and key in DIC determine if a key is included
4. Items and Iteritems: Gets a list of dictionary elements for the traversal of a dictionary, which is the keyvaluepair<key,value> of. NET, which is implemented by iterators. In general, iterators are more efficient.
>>> workmsg = {'Frank':'Coder','Nancy':'HR','Vincent':'Project Manager'}>>>Workmsg.items () [('Frank','Coder'), ('Vincent','Project Manager'), ('Nancy','HR')]>>>list (Workmsg.iteritems ()) [('Frank','Coder'), ('Vincent','Project Manager'), ('Nancy','HR')]>>>
5.keys and Iterkeys as well as values and itervalues are respectively implemented to go to the key list and the values list.
6.pop (key) and Popitem and Del Implement deletion of field elements
7.update (Parma) A dictionary to update another dictionary.
three. Summary:
On the whole, the dictionary is quite simple. However, the internal call method of the clear () and update () is the current memory of the data operation, if the individual through = To assign value can also achieve the update effect, in fact, the principle is not the same,
The = number is equivalent to discarding the previous data back in the new memory store. This kind of class is that we often update the database data, can be implemented by update, or the same principle can be implemented by delete and Add.
. NET Programmer's Python basic tutorial learn----dictionary usage [third day]