Like a list, a "dictionary" is a collection of many values. But unlike the subscript of a list, the index of a dictionary can use many different data types, not just integers. The index of a dictionary is called a "key", and the key and its associated value are called "key-value" pairs.
The table entries in the dictionary are not sorted. So you can't slice like a list.
>>> dict={' name ': ' Tom ', ' age ': + ' Color ': ' White '}>>> dict[' age ']18
Keys (), values (), and items () methods
Ways to ▎ 3 dictionaries:
Keys () key
VALUES () value
Items () key-value pairs
The values returned by these methods are not real lists and they cannot be modified.
>>> dict={' name ': ' Tom ', ' age ': +, ' Color ': ' White '}>>> for K in Dict.keys (): ... print (k) ... Colornameage>>> for V in Dict.values (): ... print (v) ...whitetom18>>> for k,v in Dict.items (): ... Print (k,v) ... Color Whitename Tomage 18
In and not in
The in and not in operators can be used to check if a value exists in the list.
>>> dict={' name ': ' Tom ', ' age ': + ', ' Color ': ' White '}>>> ' age ' in Dict.keys () true>>> ' age ' in Dict # # equals "' Age ' in Dict.keys ()" true>>> ' Color ' not in Dict.keys () false>>> ' White ' in Dict.values () True
Get () method
Before accessing the value of a key, check that the key exists in the dictionary, you can use the Get () method, which has two parameters: the key to get its value, and the alternate value to return if the key does not exist.
>>> dict={' name ': ' Tom ', ' Age ': ' "Color ': ' White '}>>> ' My hair is ' +str (dict.get (' Hair ', ' BLACK ') + ' .‘‘ My hair is BLACK. '
SetDefault () method
The SetDefault () method sets a default value for a key in the dictionary that is used when the key does not have any value.
The first argument passed to the SetDefault () method is the key to check. The second parameter is the value to set if the key does not exist. If the key does exist, the method returns the value of the key.
>>> dict={' name ': ' Tom ', ' age ': + ' Color ': ' White '}>>> dict.setdefault (' Hair ', ' black ') ' black ' > >> dict.setdefault (' age ', ' a ') 18>>> dict{' Color ': ' White ', ' name ': ' Tom ', ' age ': ' Hair ': ' Black '}
Pretty Print
If you import the Pprint module in a program, you can use the Pprint () and Pformat () functions, which will print beautifully a dictionary of words.
The Pprint.pprint () function is especially useful if the dictionary itself contains nested lists or dictionaries.
If you want nicely printed text to be a string instead of being displayed on the screen, call Pprint.pformat ().
Pprint.pprint (Somedictvalue) = Print (Pprint.pformat (somedictvalue))
>>> import pprint>>> text= ' One night can change everything in your life. ' >>> count={}>>> for char in text: ... count.setdefault (char,0) ... count[char]=count[char]+1...> ;>> Pprint.pprint (count) {': 7, '. ': 1, ' O ': 1, ' a ': 2, ' C ': 2, ' E ': 5, ' F ': 1, ' G ': 3, ' H ': 3, ' I ': 4, ' l ': 1, ' n ' : 6, ' O ': 1, ' R ': 2, ' t ': 2, ' U ': 1, ' V ': 1, ' Y ': 2}
This article is from the "garbled Age" blog, please be sure to keep this source http://juispan.blog.51cto.com/943137/1947913
[Python 3 Series] Dictionary