Python mapping type dictionary (dict)

1 Overview

A dictionary is an unordered, variable and indexed collection.
The dictionary is written in curly braces and has keys and values.
The key of a dictionary can be almost any value (not including lists, dictionaries or other variable types of values ). When
numeric types are used as keys, they follow the general rules of numeric comparison: if two values ​​are equal (for example, 1 and 1.0), then both Can be used to index the same dictionary entry.(But please note that since computers store only approximate values ​​for floating-point numbers, it is unwise to use them as dictionary keys.)
The dictionary can be separated by commas Key: value To create a list enclosed in curly braces,

dict is a class whose type is defined as:

class dict(**kwarg)				# kwarg: 键(key)参数
class dict(mapping, **kwarg)	# mapping: 位置参数映射值
class dict(iterable, **kwarg)	# iterable:位置参数列表

2. How to create a dictionary

2.1 separated by commas in curly braces Key: value The right way to create

dsk = {
    
    1 : 'a', 2 : 'b', 3 : 'c'}
print(type(dsk), ' , ', dsk)
dsk2 = {
    
    'a':1, 'b':2, 'c':3}
print(type(dsk2), ' , ', dsk2)

Output:

<class 'dict'>  ,  {
    
    1: 'a', 2: 'b', 3: 'c'}
<class 'dict'>  ,  {
    
    'a': 1, 'b': 2, 'c': 3}
Process finished with exit code 0

2.2 Use dictionary comprehension to create

dsk = {
    
    x: x ** 2 for x in range(10)}
print(type(dsk), ' , ', dsk)

Output:

E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
<class 'dict'>  ,  {
    
    0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

Process finished with exit code 0

2.3 Created by the constructor of the dict class

2.3.1 Create an empty dictionary.

Use Constructorclass dict()

dsk = dict()
print(type(dsk), ' , ', dsk)

2.3.2 Create a dictionary with keyword key parameters

Use Constructorclass dict(**kwarg)

dsk = dict(one=1, two=2, three=3)
print(type(dsk), ' , ', dsk)

Output:

E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
<class 'dict'>  ,  {
    
    'one': 1, 'two': 2, 'three': 3}
Process finished with exit code 0

2.3.3 Use iterable data objects to create a dictionary

Use Constructorclass dict(iterable)

The positional parameter is an iterable object, each item in the iterable object itself is an iterable object containing exactly two elements , then the first object in each item will become a key of the new dictionary, the second The object will become its corresponding value.

If a key appears more than once, the last value of the key will become its corresponding value in the new dictionary

lst = [('two', 2), ('one', 1), ('three', 3)]
dsk = dict(lst)
print(type(dsk), ' , ', dsk)

Output:

E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
<class 'dict'>  ,  {
    
    'two': 2, 'one': 1, 'three': 3}
Process finished with exit code 0

2.3.4 Create a dictionary using mappable objects

Use Constructorclass dict(mapping)

if __name__ == "__main__":
  d = {
    
    'aa': 1, 'bb': 2, 'cc': 3}
  dsk = dict(d)
  print(type(dsk), ':' , dsk)

Output:

E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/02.py
<class 'dict'> : {
    
    'aa': 1, 'bb': 2, 'cc': 3}
Process finished with exit code 0

2.3.5 Create a dictionary using mappable objects and keyword parameters at the same time

Use Constructorclass dict(mapping, **kwarg)

if __name__ == "__main__":
  d = {
    
    'aa': 1, 'bb': 2, 'cc': 3}
  dsk = dict(d, name='wzc', age = 30, sex = 'male')
  print(type(dsk), ':' , dsk)

Output:

E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/02.py
<class 'dict'> : {
    
    'aa': 1, 'bb': 2, 'cc': 3, 'name': 'wzc', 'age': 30, 'sex': 'male'}
Process finished with exit code 0

2.3.6 Use iterable data objects and keyword parameters to create a dictionary

Use Constructorclass dict(iterable, **kwarg)

if __name__ == "__main__":
  lst = [('a',2), ('b',4), ('c', 5)]
  dsk = dict(lst, name = 'wzc', age = 30, sex = 'male')
  print(type(dsk), ':' , dsk)

Output:

E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/02.py
<class 'dict'> : {
    
    'a': 2, 'b': 4, 'c': 5, 'name': 'wzc', 'age': 30, 'sex': 'male'}
Process finished with exit code 0

[Previous page] [Next page]

Guess you like

Origin blog.csdn.net/wzc18743083828/article/details/109759092