Python dictionaries and related operations (containing examples)

Python dictionaries

Today in python will introduce a very common combination of data types - dictionary

Some examples to be appreciated by the normal operation of the dictionary

What is a dictionary type?

Find the list of search function is achieved through an integer index (number of elements in the list).

However, many applications need to find a more flexible way, it means that the key and the corresponding value of the index components.

For example: "username" Find "phone number", "study" Find "college class," etc.

Find a set of data value information by any key information process is called "mapping"

Python language, map them through the dictionary.

It is simple to understand, and to process information dictionary information corresponding relationship mapping combinations of data types.

Dictionary type of operation

Create a dictionary content for the country and capital of the mapping, and add China, the United States, France, for example

# The first way to add 
d = { " China " : " Beijing " , " America " : " Washington " }
 # The second way to add 
d [ " France " ] = " Paris " 
Print (d)

 

It should be noted that, as dictionaries and collections have the disorder, the returned results may be different and the input sequence

The main structure of a dictionary: the key corresponding to the value ;

In the above example, China, the United States, France, the equivalent keys, Beijing, Washington, Paris belongs value.

1.keys()

Function to return all the role of "key" information, that is, return to "China, the United States, France," any parameters, using the method does not pass as follows:

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print (d.keys ())

 

The results are as follows:

 

 

 Tip: You can list the results in a manner of input, only need to add list () to change my

2.values()

Similar to the role and function keys (), the role of information for the return value of the dictionary, which is the return to "Beijing, Washington, Paris," does not require the same parameters, as follows

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print (d.values ())

 

The results are as follows:

3.items()

 

Its main role is to return all of the dictionary key-value pairs, and direct printing looks dictionary itself did not differ much, that under special to compare the effect of:

 

 

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " } 
# Direct Print
Print (d)
# key to print
Print (d.items ())

 

 

 

 

Results are as follows:

 

 

 Obviously, by items () to print key, key-value pairs will pass () to represent the relationship

4.get(<key>,<default>)

get () function determination function determines if the key has a value corresponding to, if there is a corresponding value corresponding to the return value, if there is a parameter value is returned, if no input parameter values, the default is none

For example child: Enter key - China, there is value - Beijing Returns "Beijing", enter key - Thailand, there is no value, none is returned

 

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 # key has a corresponding value of China, returned to Beijing 
Print (d.get ( " China " ))
 # key to Thailand no values, no default value passed to return none 
Print (d.get ( " Thailand " ))
 # key to Italy no values, there is an incoming default value of "no such data dictionary" return to the default value of "no such dictionary data " 
Print (d.get ( " Italy " ," Dictionary is no such data ."))

 

 

 

The results are as follows:

 

 

 

5.pop(<key>,<default>)

pop () function to get () is similar, using the same method, however, pop () function on the basis of get () on the pay more step, when the bond is present then returns the corresponding value while deleting the corresponding key, if there with get ()

 

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print (d)
 Print (d.pop ( " France " ))
 Print (d)

 

 

 

The effect is obvious, the first original print dictionary, print dictionary by pop delete it and then, as shown below, France key-value pairs disappear ()

 

 

 6.popitem ()

Function operates to randomly select a key-value pair from the dictionary and returns it as a tuple (key, walue) of

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print (d.popitem ())

 

7.clear()

Like English literal translation, as the role of clear () deletes all key-value pairs, that is, to clear all internal data dictionary

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print (d)
d.clear()
print(d)

 

The results are as follows:

8.del<d>[<key>]

 

Operation for incoming key, delete the corresponding pairs.

 

 

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print (d)
 del d [ " France " ]
 Print (d)

 

 

 

 

The results are:

 

9<key>in<d>

Belonging judgment function returns true if the bond is in the dictionary false otherwise

= {d " China " : " Beijing " , " America " : " Washington " , " French " : " Paris " }
 Print ( " United States " - in d)

 

Dictionary practical exercise

Example 1

D = { "John Doe": 88, "John Doe": 90, "Wang Wu": 73, "Zhao VI": 82}

1. Add key-value pairs in the dictionary "money Seven": 90

= {D " Joe Smith " : 88, " John Doe " : 90, " Wang Wu " : 73, " Zhao six " : 82 }
D [ " Qian's Seven " ] = 90
 Print (D)

 

2. Modify the king five information 93

= {D " Joe Smith " : 88, " John Doe " : 90, " Wang Wu " : 73, " Zhao six " : 82 }
D [ " Wang Wu " ] = 93
 Print (D)

 

3. Delete Zhao six corresponding key-value pairs

= {D " Joe Smith " : 88, " John Doe " : 90, " Wang Wu " : 73, " Zhao six " : 82 }
D.pop("赵六")
print(D)

 

Example 2:

Enter a string of string, count the number of times each letter appears

INPUT = S ( " Input: " )
d={}
for i in s:
    if i not in d :
        d [i] = 1
     else :
        d[i]=d[i]+1
print(d)

 

Guess you like

Origin www.cnblogs.com/lyy135146/p/11772998.html