Basic operation python_ dictionary

Dictionary of increase:

1.  (dictionary name) [ 'Key'] = 'value'      example: to the dictionary dic added to a set of data     dic [ ' Big Brother Po '] = ' so cool '

# This method if key duplication operation will be carried out with the new

2.  (dictionary name) .setdefault ( 'key', 'value') dic.setdefault ( ' big brother Po ', ' so cool ')   # if the dictionary already contains the data you want to increase the key is not to save it If a value the value is not entered, the default null value ## of this two-step operation. (1) adding to the dictionary key and value (if it already exists does not affect the value of the value of the corresponding key)

                        (2) returns the value corresponding to the value of the key value

 

 Delete the dictionary:

Pop: dic.pop ( 'key') # can be ejected using a variable load  

From the dock [ 'key']

Popitem () dic.popitem () # random delete a

Clear () dic.clear () empty dictionary

 

Dictionary queries

Dic [ 'key'] # When the key is not being given example: DIC [ ' Master Yi ']   blademaster

Dic.get ( 'Key' (, ' (do not look for the default value when you want to modify the default is none )) ')  

 Examples: dic.get ( ' Master Yi ')    blademaster              dic.get ( ' boy ') # list No    none  

        Dic.get ( ' boy ' , 'No ah!' )   No ah!

 

Dic.setdefault # 1. First determine the original dictionary, there is no such Key , if not, add execution

                               2. Use this key to go to the dictionary query, return query results to

 ### pictures can not put orz. .

The object of the program is added to the dictionary is greater than 66 and less than 66 numbers and which are placed with k1 and k2 two key in, dic.setdefault () first because not in the dictionary k1 is present, it creates a direct new Key , and returns K1 , and when iterating again, dic.setdefault () returns the K1 (because the dictionary has been K1 , it will not add to the list), thus achieving the title of the effect to be achieved

 

 

 

 

 

### if the direct iteration of the dictionary, you'll get all the dictionary Key . for ...

 

 

Dictionary modifications: 1. DIC [ existing key] = new value

                       2.update d1.update (d2) the dictionary d2 of kv update the dictionary d1 the unit dictionary ##

 

 Other common operations:

Dic.keys ()   Search Keys   # return key collection like a list, but not a list, but can be an iterative loop ( for )

Dic.values () to query all values    # The same list also like to be. . .

Dic.items ()   query the dictionary all the elements   # pretending to be a tuple. Back to list

 

# Deconstruction, unpack operation:   A, B = (1,2) the 1 and 2 respectively assigned to A , B .

By   K , V = dic.items () to key and the value acquired! ! !

 

Dictionary nesting to be set. . . Omission

# Dictionary delete operation can not be performed when the cycle of the dictionary

 the above mentioned id ## () returns the memory address of the object when the object is a list, a tuple, when the dictionary, the same content will not return to the same memory address, and when the object is just a single object will return the same memory address, because there is little data pool , the string will be cached (to save space)

Guess you like

Origin www.cnblogs.com/lingwc/p/11580559.html