Dictionary Description:
In Python, a dictionary is a series of key-value pairs. Each key is associated with a value, and you can use the key to access the value associated with it. The value associated with the key can be a number, a string, a list, or even a dictionary. In fact, any Python object can be used as a value in the dictionary
Defined:
You can define a dictionary in several ways:
dic = { " color ": " green , " points ": 5}dic1 = dict (color = " green ", points = 5) Dic2 = Dict ([ " Color ", " Green "), ( ' Points ", 5)])
Dictionary derivation you can create a dictionary from any key-value expression
for in range (2, 2)}print(DIC) # {2:4, 4:16, 6:36, 8:64, 10:100}
Circular Dictionary
When looping through a dictionary, keywords and corresponding values can be interpreted simultaneously using the items () method:
DIC = {'color'green'points': 5 } for in Dic.items (): print(key, value)# output # color green# points 5
You can also use the keys () method to remove only the keys from the dictionary and use the values () method to remove the value from the dictionary.
Method
defClear (self):#no return value, delete all items in the dictionary """d.clear (), None. Remove all items from D.""" Pass defCopy (self):#Shallow copy of the dictionary """d.copy (), a shallow copy of D""" Pass defFromkeys (*args, **kwargs):#function is used to create a new dictionary, which is the key to the dictionary of elements in the sequence SEQ, and value is the initial value corresponding to all keys in the dictionary """Returns a new dict with keys from iterable and values equal to value.""" Pass defGet (self, k, d=none):#returns the value of the given key value if none exists. """D.get (K[,d]), D[k] if k in D, else D. D defaults to None. """ Pass defItems (self):#returns a list of key-value pairs """D.items () a Set-like object providing a view on D ' s items""" Pass defKeys (self):#back to List of keys """D.keys () a Set-like object providing a view on D ' s keys""" Pass defPop (self, k, d=none):#deletes the given key-value pair, and returns the value corresponding to the key, if there is no keyerror error generated """D.pop (K[,d])-V, remove specified key and return the corresponding value. If key is no found, D is returned if given, otherwise keyerror is raised""" Pass defPopitem (self):#Delete a key--value pair, and return the key value pair, random delete, if NULL, return keyerror error """D.popitem (), (k, v), remove and return some (key, value) pair as a 2-tuple; but raise keyerror if D is empty. """ Pass defSetDefault (self, k, d=none):#if key is in the dictionary, the corresponding value is returned. If it is not in the dictionary, insert key and set the default value defaults, and return to default, which defaults to None. """D.setdefault (K[,d]), D.get (K,d), also set D[k]=d if K not in D""" Pass defUpdate (self, E=none, **f):#update the key/value pairs of the dictionary dict2 to the Dict """d.update ([E,]**f), None. Update D from Dict/iterable E and F. If E is present and have a. Keys () method, then Does:for k in e:d[k] = E[k] If e is present and lacks a. Keys () m Ethod, then does:for K, v in e:d[k] = V In either case, the is followed By:for k in f:d[k] = F[k] """ Pass defVALUES (self):#returns a list of dictionary values """d.values () A object providing a view on D ' s values""" Pass
View Code
Python Basics-dictionaries and ordered dictionaries