A dictionary consists of a key-value pair, consisting of a key (name) and a value that differs from the list and tuple is that the element inside is a key (name), So when you want to print the dictionary, print the dictionary variable plus [Key] Can
The dictionary comes with a function:
Function |
Function |
Clear () |
Delete all elements in a dictionary |
Copy () |
Returns a copy of the dictionary (shallow copy) |
Get (Key) |
The key to the dictionary dict, returns the value it corresponds to, and returns the value of default if the key does not exist in the dictionary |
Items () |
Returns a list that contains a tuple (key, value) in the dictionary |
VALUES () |
Returns a list that contains all the values in the dictionary |
Key () |
Returns a list containing the keys in the dictionary |
Len (self) |
Returns the number of keys in Dict |
Pop (key) |
Similar to method get (), if the key key exists in the dictionary, delete and return Dict[key], if the key key does not exist and the value of default is not given, the Keyerror exception is thrown |
Del () |
Del dictionary variable. [Element key to delete] |
Note : Parameters:
K: Key to search for
D: Default is NULL, returns the default value if the key does not exist
The function operates as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-dict={"Zou": 2,"Lu"75A"Chen": 6}a=dict.copy ()Print(a)#print out {' Chen ': 6, ' Lu ': 5, ' Zou ': 2}b=["Bob","John"]Print(Dict.fromkeys (b,8))#Print new Dictionary {' Bob ': 8, ' John ': 8}Print(Dict.get ("Zou"))#Printing 2Print(Dict.items ())#Print [(' Chen ', 6), (' Lu ', 5), (' Zou ', 2)]Print(Dict.keys ())#print [' Chen ', ' Lu ', ' Zou ']Print(Dict.values ())#Print [6, 5, 2]Print(Dict.pop ("Lu"))Print(dict)#Print {' Chen ': 6, ' Zou ': 2} Delete {"Lu": 5}Print(Len (dict))#print back the number of keys in Dictdict.clear ()Print(dict)#Print {} Empty dictionary
Common actions : index new Delete key, value, key value pair loop length
# !/usr/bin/env python # -*-coding:utf-8-*-dict={"Zou": 2,"lu": 5," Chen": 6}print(dict["lu"]) # print "Lu" key 5
For loop
#!/usr/bin/env python#-*-coding:utf-8-*-dict={"Zou": 2,"Lu"75A"Chen": 6} forBinchdict:Print(b)#Print all the keys of Dict Chen Lu Zou#because the dictionary is unordered, the key order of the printed dictionary is different from the original dictionary forCinchdict:Print(Dict[c])#Print out 6 5 2
(items: Get keys and values) combined for loop
#!/usr/bin/env python#-*-coding:utf-8-*-dict={"Zou": 2,"Lu"75A"Chen": 6} forKvinchDict.items ():#because the items () get the keys and values, so in the loop to customize two variables representing the key and the value Print(k)Print(v)#Print Chen 6 lu 5 Zou 2 loop out the dictionary keys and Values
Verify that the keys and values are in the dictionary:
#!/usr/bin/env python#-*-coding:utf-8-*-dict={"Zou": 2,"Lu"75A"Chen": 6}a="Zou" inchDict.keys ()Print(a) b=2inchdict.values ()Print(b)
Update (self, e=none)
(Append update) to append updated variables
#!/usr/bin/env python#-*-coding:utf-8-*-dict={"Zou": 2,"Lu"75A"Chen": 6}b={"Bob": 1,"John": 4,"Lin": 3}dict.update (b)Print(dict)#Print {' Lu ': 5, ' Zou ': 2, ' Chen ': 6, ' Bob ': 1, ' John ': 4, ' Lin ': 3}
Delete the elements in the dictionary
format:del dictionary variable. [Element key to delete]
# !/usr/bin/env python # -*-coding:utf-8-*- b={" bob ": 1, " john ": 4," lin ": 3} del b[ john " " print (b) # print {' Bob ': 1, ' Lin ': 3} Delete "John" key
Some common methods:
__contains__ (...)
D.__contains__ (k)-True if D has a key k, else False
if the dictionary D contains the key K, then returns True, otherwise false
Similar functions:
Has_key (...)
D.has_key (k)->true if D has a key K, else false# returns True if dictionary D has key K, otherwise False
Items (...)
D.items ()->list of D ' s (key, value) pairs, as 2-tuples
#返回包含D的键值对的列表list, the element in list is a tuple with 2 data
Iteritems (...)
D.iteritems ()->an iterator over the (key, value) items of d# return an iterator object, each element is a key value pair (key, value)
Iterkeys (...)
D.iterkeys () A iterator over the keys of d# returns an iterative object with each element being a key to dictionary D
Itervalues (...)
D.itervalues-A iterator over the values of d# returns an iterative object with each element being a value of dictionary D
Functions of the Dictionary
Index
Increase
Delete
For loop
Length
Item
Key
Value
Determine if an element exists
Update
11th lesson of Python Learning-basic data type (DICT)