Python映射类型之字典(dict)

1. 概述

字典是一个无序、可变和有索引的集合。
字典用花括号编写,拥有键和值。
字典的键 几乎 可以是任何值(单不包括列表、字典或其他可变类型的值
数字类型用作键时遵循数字比较的一般规则:如果两个数值相等 (例如 1 和 1.0) 则两者可以被用来索引同一字典条目。(但是请注意,由于计算机对于浮点数存储的只是近似值,因此将其用作字典键是不明智的。)
字典可以通过将以逗号分隔的 键: 值 对列表包含于花括号之内来创建,

dict是一个类,其类型定义为:

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

2. 创建字典的方法

2.1 在花括号内以逗号分隔的 键: 值 对的方式创建

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

输出:

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

2.2 使用字典推导式创建

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

输出:

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 通过dict类的构造器来创建

2.3.1 创建一个空字典。

使用构造器class dict()

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

2.3.2 通过关键字键参数,创建字典

使用构造器class dict(**kwarg)

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

输出:

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 使用可迭代数据对象创建字典

使用构造器class dict(iterable)

位置参数为一个 iterable 对象,该可迭代对象中的每一项本身为一个刚好包含两个元素的可迭代对象,那么每一项中的第一个对象将成为新字典的一个键,第二个对象将成为其对应的值。

扫描二维码关注公众号,回复: 12123063 查看本文章

如果一个键出现一次以上,该键的最后一个值将成为其在新字典中对应的值

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

输出:

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 使用可映射对象创建字典

使用构造器class dict(mapping)

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

输出:

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 同时使用可映射对象和关键字参数创建字典

使用构造器class 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)

输出:

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 同时使用可迭代数据对象和关键字参数创建字典

使用构造器class 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)

输出:

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

[上一页][下一页]

猜你喜欢

转载自blog.csdn.net/wzc18743083828/article/details/109759092