Python basic syntax (10) dictionary of basic types

Keywords: Python Back-end

Basic Python syntax (1) introduction to Python

Python basic grammar (2) Zen of Python

Python basic syntax (3) use of miniconda

Python basic syntax (4) install Python language based on CentOS7 source code

Python basic syntax (5) numbers of basic types

Python basic syntax (6) string of basic types

Python basic syntax (7) list of basic types

Python basic syntax (8) tuples of basic types

Python basic syntax (9) collection of basic types

Python basic syntax (10) dictionary of basic types

Definition and characteristics of dictionary dict

  • (1) The dictionary is also enclosed in curly braces. Unlike the set, the elements in the dictionary are key value pairs
>>> a={"a":1,"b":2}
>>> a
{'a': 1, 'b': 2}
>>> type(a)
<class 'dict'>

2. Common operations of Dict

  • (1) When the dictionary fetches elements, it fetches them through the key
>>> a={"a":1,"b":2}
>>> a
{'a': 1, 'b': 2}
>>> a["a"]
1
>>> a["b"]
2
  • (2) The len() function calculates the number of key value pairs in the dictionary
>>> a={"a":1,"b":2,"c":3,"d":4}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> a
{'a': 10, 'b': 2, 'c': 3, 'd': 4}
>>> len(a)
4
  • (3) The max() function calculates the maximum value of the key in the dictionary
>>> a={"a":1,"b":2,"c":3,"d":4}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> a
{'a': 10, 'b': 2, 'c': 3, 'd': 4}
>>> max(a)
'd'
  • (4) The min() function calculates the minimum value of the key in the dictionary
>>> a={"a":1,"b":2,"c":3,"d":4}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> a
{'a': 10, 'b': 2, 'c': 3, 'd': 4}
>>> min(a)
'a'
  • (5) in, not in judge whether there is a given key in the dictionary according to the key of the dictionary
{'a': 10, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> "a" in a
True
>>> 10 in a
False
>>> "b" in a
True
>>> 10 not in a
True

3 common functions in dictionary

  • (1) clear() to clear the dictionary
>>> a={"a":1,"b":2}
>>> a
{'a': 1, 'b': 2}
>>> a.clear()
>>> a
{}
  • (2) copy() returns a shallow copy of the dictionary
>>> a={"a":1,"b":[1,2,3]}
>>> b=a.copy()
>>> a
{'a': 1, 'b': [1, 2, 3]}
>>> b
{'a': 1, 'b': [1, 2, 3]}
>>> a["b"].append(4)
>>> a
{'a': 1, 'b': [1, 2, 3, 4]}
>>> b
{'a': 1, 'b': [1, 2, 3, 4]}
>>> a["a"]=100
>>> a
{'a': 100, 'b': [1, 2, 3, 4]}
>>> b
{'a': 1, 'b': [1, 2, 3, 4]}
  • (3) From keys (iterable, value = None). Create a new dictionary from the value in iterable as a key. The value corresponding to each key is value. If it is not specified, it is None by default
>>> a=[1,2,3,4,5]
>>> b=dict.fromkeys(a)
>>> b
{1: None, 2: None, 3: None, 4: None, 5: None}
>>> c=dict.fromkeys(a,100)
>>> c
{1: 100, 2: 100, 3: 100, 4: 100, 5: 100}
  • (4) get(key,default=None) to get the value corresponding to a given key from the dictionary. If there is no such key in the dictionary, the default value can be given by default. If the default value is not given, the default value is None
>>> a={"a":1,"b":2,"c":3,"d":5}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 5}
>>> b=a.get("a")
>>> b
1
>>> b=a.get("a",10)
>>> b
1
>>> b=a.get("f")
>>> b
>>> b=a.get("f",100)
>>> b
100
  • (5) pop(key,default=None) pops up the value corresponding to the given key from the dictionary. If the key is not found in the dictionary, the value specified by default is returned. If the key is not found in the dictionary and default is not specified, a KeyError exception is reported
>>> a={"a":1,"b":2,"c":3,"d":4,"e":5}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> b=a.pop("a")
>>> b
1
>>> a
{'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> b=a.pop("f",100)
>>> b
100
>>> a
{'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> b=a.pop("f")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'f'
  • (6) update(list or dict) to update a binary list or dictionary to the existing dictionary
>>> a={"a":1,"b":2}
>>> b={"c":3,"d":4}
>>> a
{'a': 1, 'b': 2}
>>> b
{'c': 3, 'd': 4}
>>> a.update(b)
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> b
{'c': 3, 'd': 4}
>>> c=[(1,2),(3,4)]
>>> a.update(c)
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 1: 2, 3: 4}
  • (7) popitem() pops up a (key, value) binary from the dictionary. If the dictionary is empty, a KeyError exception is reported
>>> a={1:2,3:4,5:6}
>>> a
{1: 2, 3: 4, 5: 6}
>>> b=a.popitem()
>>> b
(5, 6)
>>> a
{1: 2, 3: 4}
>>> c=a.popitem()
>>> c
(3, 4)
>>> a
{1: 2}
>>> d=a.popitem()
>>> d
(1, 2)
>>> a
{}
>>> e=a.popitem()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
  • (8) setdefault(key,default=None) if the key does not exist in the dictionary, add the key default to the dictionary. If the key exists in the dictionary, return the value corresponding to the key. Otherwise, return the set default value
>>> a={1:2,3:4}
>>> b=a.setdefault(2)
>>> b
>>> a
{1: 2, 3: 4, 2: None}
>>> b=a.setdefault(4,5)
>>> b
5
>>> a
{1: 2, 3: 4, 2: None, 4: 5}
>>> b=a.setdefault(1,100)
>>> b
2
>>> a
{1: 2, 3: 4, 2: None, 4: 5}
  • (9) keys() returns an object of the dictionary key list, which can be converted into a list of keys through the list method
>>> a={"a":1,"b":2,"c":3}
>>> b=a.keys()
>>> b
dict_keys(['a', 'b', 'c'])
>>> type(b)
<class 'dict_keys'>
>>> list(b)
['a', 'b', 'c']
  • (10) values() returns an object of the dictionary value list, which can be converted to a list of values through the list method
>>> a={"a":1,"b":2,"c":3}
>>> b=a.values()
>>> b
dict_values([1, 2, 3])
>>> type(b)
<class 'dict_values'>
>>> list(b)
[1, 2, 3]
  • (11) items() returns an object of the dictionary value value pair, which can be converted into a list of key values through the list method
>>> a={"a":1,"b":2,"c":3}
>>> b=a.items()
>>> b
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>> type(b)
<class 'dict_items'>
>>> list(b)
[('a', 1), ('b', 2), ('c', 3)]

Posted by dcgamers on Fri, 12 Nov 2021 11:32:34 -0800