python基础-数据类型2-字典、集合 相关操作与公共方法

本文详细介绍了Python中的字典操作,包括定义、增加、删除、修改、查询以及遍历,并强调了键的唯一性和见名知意的原则。同时,讲解了集合的特性,如无序和不重复,以及集合的增删和判断操作。还探讨了加法和乘法运算在不同类型数据之间的应用规则。
摘要由CSDN通过智能技术生成

9、字典的定义

  • 格式:变量 = {key1 : value1, key2: value2…}
  • 空字典定义:
    • {}
    • dict()
  • 字典中键不能重复,是唯一的,但是值可以重复
  • 字典中的键要见名知意,体现字典可以见名知意的特性
# 字典:储存多个数据,以键值对形式存储,方便快速存取
# 字典的键要见名知意

# 字典定义格式: 变量 = {键1:值1, 键2:值2.....}
dict1 = {'name': 'xiaoming', 'age': 18, 'gender': '女'}
# 使用print打印可以显示字典中的所有数据
print(dict1)
# 查看字典类型
print(type(dict1))  # <class 'dict'>

# 空字典定义方法
dict2 = {}
# 或者
dict3 = dict()
print(dict2, dict3)
print(type(dict2), type(dict3))

# 见名知意的重要性
# 需求: 使用字典保存一个人的信息  xiaoming  18   男  001
# 保存方式一:
# dict4 = {'name': 'xiaoming', 'age': 18, 'gender': '男', '学号': '001'}
# print(dict4)
# 保存方式二:
# 字典的优势是快速存取,注意命名键的时候要见名知意,并且易于记忆
# 字典占用空间远大于列表,使用字典存储数据本来就是牺牲空间确保时间,所以要尽量利用字典快速存取的特性,而不要想空间的节省
# dict5 = {'xiaoming':18, '男':'001'}  # 不建议这样写

# 定义字典时 ,不能有重复的键,否则后定义的键值对会覆盖先定义的

dict6 = {'name': 'xiaoming', 'age': 18, 'name': 'rose'}
# 字典中的键是惟一的,后定义的内容值会覆盖先定义的
print(dict6)

# 字典中键是唯一的但是值可以随意重复
dict7 = {'name': '小明', 'age': 18, 'id': 18}
print(dict7)

10、字典的增加

  • 字典[新的key] = 值
  • 如果key在原字典中已经存在则为修改原key对应的值
# 增  使用新的键 = 值的形式增加键值对
dict1 = {'name':'xiaoming', 'age': 18}
# 使用新的键= 值
# 格式:字典变量[key] = 值  如果为新增,则key在原字典中不存在
dict1['gender'] = '男'
print(dict1)  # {'name': 'xiaoming', 'age': 18, 'gender': '男'}

# 如果原字典中存在该key 则为修改原key所对应的值
dict1['name'] = 'xiaowang'
print(dict1)  # {'name': 'xiaowang', 'age': 18, 'gender': '男'}

# update
# 一般用于两个字典间的拼接
# 如果update中添加的键已经存在则修改原有的值
dict1.update({'id': '001', 'color': 'yellow', 'name': 'rose'})
print(dict1)

11、字典的删除

  • del 查找到字典的键所对应的值进行删除
  • clear()清空字典所在数据空间中的多有键值对
  • pop:删除指定键所对应的键值对,会将删除的键值对所对应的值进行返回
  • popitem: 删除随机一个键值对,尝试后发现总是删除最后一个,会将删除的键值对以元组的形式进行返回
# del
# 使用del删除键值对,先要找到dict所对应的键,进行删除
# 注意,在字典中键值对是成对出现的,删除键值也就消失了,不能出现单独的键或者单独的值
dict1 = {'name': 'xiaoming', 'age': 18}
del dict1['age']
print(dict1)  # {'name': 'xiaoming'}

# clear() 清空字典
# 使用clear将字典所对应的内存空间中的数据进行了清空
dict1.clear()
print(dict1)  # {}

# 通过之前的学习经验我们猜测 pop是删除简直对用的
dict2 = {'name': 'xiaoming', 'age': 18, 'gender': '男'}
# 使用pop可以根据指定的key删除键值对
# 使用pop删除键值对后会将其键对应的值进行返回
# print(dict2.pop('name'))  # xiaoming
# print(dict2)  # {'age': 18, 'gender': '男'}

# 猜测:popitem也是删除键值对使用的
# 随机删除一个键值对,一般都是删除最后一个
# 删除后会将我们所删除的键值对以元组的形式进行返回
print(dict2.popitem())  # ('gender', '男')
print(dict2.popitem())  # ('age', 18)
print(dict2)  # {'name': 'xiaoming'}

# dict  无序的  因为其不能通过索引进行键值对的获取(了解)
# Python3.5以后,字典中键值对的顺序和我们插入键值对的顺序保持一致,但是该顺序没法被利用(了解)

12、字典的修改

  • 字典[key] = 值
    • 字典中key必须存在
  • update:
    • update(键 = 值)
    • update({键:值})
    • 对应的键一定存在
# 通过索引修改字典中的键值对
dict1 = {'name':'小明', 'age':18}
dict1['name'] = '小红'
print(dict1)

# update
# 可以进行制定字段值的修改
# dict1.update(name='小绿')
dict1.update({'name': '小绿'})
print(dict1)

13、字典的查询

  • 使用键查询值:字典[key]
    • 查询的键不存在时则报错
  • get:字典.get(key)
    • 查询的键不存在时,不报错,可以默认返回None,或者手动设置返回内容
  • keys:获取所有的键
  • values:获取所有的值
  • items:获取所有的键值对组成的元组
# 直接使用key进行查询
dict1 = {'name': '小明', 'age': 18, 'gender': '男', 'id': '001'}
# 查询学员的名称?
print(dict1['name'])

# get查询
# 如果我们查询的键不存在会真么样??? 报错
# KeyError: 'apple'  会出现keyerror  表示查询的键不存在  报错
# print(dict1['apple'])
# 使用get进行查询,只需要在get中传入对应的键即可
# 如果使用get查询的键不存在,则不会报错,会默认返回一个None
print(dict1.get('name'))  # 小明
print(dict1.get('apple'))  # None
# 如果查询的键不存在,get可以自定义默认返回值
# 格式 字典.get(要查询的键, 查询的键不存在时返回的数据)
print(dict1.get('apple', '小刚'))
print(dict1.get('apple', 9))

# keys 获取当前字典中所有的键
print(dict1.keys())  # dict_keys(['name', 'age', 'gender', 'id'])
# keys返回的内容不是列表,而是dict_keys,该数据类型不能直接使用索引查询数据,但是可以进行for遍历
print(type(dict1.keys()))  # <class 'dict_keys'>
keys_1 = dict1.keys()
#  不能使用索引查询
# TypeError: 'dict_keys' object is not subscriptable
# print(keys_1[1])
# 可以被迭代
for i in keys_1:
    print(i)

# values 获取当前字典中所有的值
print(dict1.values())  # dict_values(['小明', 18, '男', '001'])
# dict_values不能使用索引查询,但是可以迭代
print(type(dict1.values()))  # <class 'dict_values'>

# items 获取当前字典中所有的键值对,键值对以元组形式展示
print(dict1.items())  # dict_items([('name', '小明'), ('age', 18), ('gender', '男'), ('id', '001')])
# dict_items不能使用索引查询,但是可以迭代
print(type(dict1.items()))  # <class 'dict_items'>

14、字典的遍历

# 字典的遍历
dict1 = {'name': '小明', 'age': 18, 'gender': '男', 'id': '001'}

# 使用for循环对字典进行遍历,默认获取的是字典的每一个键
for i in dict1:
    print(i)
'''
name
age
gender
id
'''
# 获取的是字典的每一个键
for i in dict1.keys():
    print(i)
'''
name
age
gender
id
'''

# 获取的是字典的每一个值
for i in dict1.values():
    print(i)
'''
小明
18
男
001
'''

# 获取的是字典中每一个键值对组成的元组
for i in dict1.items():
    print(i)
'''
('name', '小明')
('age', 18)
('gender', '男')
('id', '001')
'''
# 有没有办法可以分别拿到字典的键和值呢?

for i in dict1:
    print(i, dict1[i])

for key, value in dict1.items():
    print(key, value )

15、集合的定义

  • 变量 = {数据1, 数据2, 数据3.。。。}
  • 空集合:set()
  • 集合是一个无序的 不重复的数据序列
# 集合: 集合是一个无序,不重复的数据序列
# 无序: 程序员无法控制其排不顺序,  程序员无法使用索引查找或修改数据
# 不重复:没有办法在字典中放入相同的值,会自动去重,类似于字典的键

# 无序:
set1 = {1, 2, 5, 6, 3, 4}
# 程序员无法利用其顺序,有顺序也无用
# 了解:在集合中会使用数据的值计算哈希值,根据哈希值顺序进行排序
print(set1)  # {1, 2, 3, 4, 5, 6}

# 不重复
set2 = {1, 2, 3, 4, 5, 6, 7, 2, 3}
# set会自动去重
print(set2)  # {1, 2, 3, 4, 5, 6, 7}

# 定义空集合
set3 = set()
# {} 是定义空字典的
print(set3)

# 集合中能够储存什么数据?
# 布尔值在进行计算时  True == 1 Fasle == 0
# 基础数据类型 int  float  bool  字符串 都可以用集合储存
set4 = {1, 12.3, True, 0, False, ''}
print(set4)

# TypeError: unhashable type: 'list'
# 列表数据无法用集合储存
# set5 = {1, 12.3, True, 0, False, '', [1, 2]}
# print(set5)

# 元组类型可以放入集合内储存
set6 = {1, 12.3, True, 0, False, '', (1, 2)}
print(set6)

# TypeError: unhashable type: 'dict'
# 字典类型无法用集合储存
# set6 = {1, 12.3, True, 0, False, '', {1:2}}

# TypeError: unhashable type: 'set'
# 集合类型同样不能使用集合嵌套
# set6 = {1, 12.3, True, 0, False, '', {1,2}}

# 结论:列表  字典  集合,不能放入集合中,作为元素出现

# 拓展:不能作为集合元素的数据类型,同样不能作为字典的键出现
dict1 = {(1, 2): 3}
print(dict1)
# TypeError: unhashable type: 'list'
# 列表 字典 集合不能作为字典的键出现
dict2 = {[1, 2]: 3}
print(dict2)

16、集合的相关操作

  • 集合的增加
    • add:添加一个元素,如果值已存在,则去重
    • update: 更新元素(在括号中添加可迭代类型),如果值已存在则去重
# add 增加
set1 = {1, 2, 3, 4}
# set 在使用add命令后,不会产生新的数据,而是原集合中进行修改
set1.add(5)
print(set1)  # {1, 2, 3, 4, 5}

# update 更新
# TypeError: 'int' object is not iterable
# update内部只能填写容器类型(数据序列)
# set1.update(6)
set1.update([6, 7])
print(set1)  # {1, 2, 3, 4, 5, 6, 7}
# 如果更新的数据已经存在,则去重
set1.update([1,2])
print(set1)  # {1, 2, 3, 4, 5, 6, 7}
  • 集合的删除
    • remove:根据元素值进行删除,如果元素不存在则报错
    • discard:根据元素值进行删除,如果元素值不存在则不报错
    • pop:删除任意元素,并返回被删除的值
# remove
set1 = {1, 2, 3, 4}
# 使用remove可以删除指定值的元素
# set1.remove(3)
# print(set1)  # {1, 2, 4}

# pop 随机删除一个元素,并且将删除的元素返回
# print(set1.pop())
# print(set1)

# discard
# 如果使用remove删除的元素不存在会怎样?  报错
# set1.remove(13)  # KeyError: 13
# 如果使用discard删除元素呢?  不会报错
set1.discard(3)
print(set1)  # {1, 2, 4}
set1.discard(13)
print(set1)
  • 集合判断:
    • in
    • not in
# 数据是否在集合中
set1 = {1, 2, 3, 4}
# in 判断元素是否在集合中出现
print(4 in set1)  # True
print(5 in set1)  # Fasle

# not in 判断元素是否不在集合中
print(4 not in set1)  # False
print(5 not in set1)  # True

# 注意:格式  元素 in  集合

# 判断的数据必须要在集合中能够被储存
# TypeError: unhashable type: 'list'
# print([1, 2] in set1)
  • 集合可以使用for循环遍历,但是遍历顺序随机
# for 遍历
set1 = {1, 2, 3, 4}
for i in set1:
    print(i)

# 注意遍历集合,顺序不定
name_set = {'Tom', 'Bob', 'Rose'}
for i in name_set:
    print(i)
'''
Rose
Tom
Bob
'''

17、公共方法

  • +
    • 加法运算适用于所有的基础数据类型(int float bool)
    • 加法运算所有两侧要是同种数据类型
    • 加法运算再容器类型中是拼接的意思,不是相加计算值
# +法运算,都可以用于哪些数据类型之间
# int float  bool 肯定可以用于加法运算,不再赘述
print(1 + 12.3)  # 13.3

# str 可以相加么? 可以
str1 = 'hello'
str2 = ' python'
# 字符串相加,可以快速将字符串进行拼接
print(str1 + str2)
# 相加过后,str1 和str2 没有发生变化,可以推断+法运算产生了新的数据,源数据不变化
print(str1, str2, sep='\n')

# list 可以相加么? 可以
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# 列表相加可以将列表进行拼接,效果类似于extend
print(list1 + list2)  # [1, 2, 3, 4, 5, 6]
# list相加后,原数据不发生变化,将产生一个新的数据
print(list1)  # [1, 2, 3]
print(list2)  # [4, 5, 6]

# tuple 可以相加么?  可以
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(tuple1 + tuple2)  # (1, 2, 3, 4, 5, 6)
# 由于元组内部元素不能修改,索引相加肯定没有对源数据产生影响,而是生成了新的数据

# set
# set1 = {1, 2, 3}
# set2 = {4, 5, 6}
# # TypeError: unsupported operand type(s) for +: 'set' and 'set'
# # set之间不能进行加法运算
# print(set1 + set2)

# dict
# TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
# dict 类型间不能进行加法运算
# dict1 = {'name': '小明'}
# dict2 = {'age':18}
# print(dict1 + dict2)

# 结论: 基础数据类型都可以进行加法运算,容器类型间只有列表,元组,字符串可以进行加法运算


# 不同容器类型间可以相加么?
list1 = [1,2,3]
tuple1 = (1,2,3)
set1 = {1,2,3}
dict1 = {'name': 'xiaoming'}

# TypeError: can only concatenate list (not "tuple") to list
# print(list1 + tuple1)
# TypeError: can only concatenate list (not "set") to list
# print(list1 + set1)
# TypeError: can only concatenate tuple (not "set") to tuple
print(tuple1 + set1)

# 结论,数据类型不同无法进行加法运算(特指容器类型之间)
  • *
    • 基础数据类型(int float bool)都可以进行乘法运算
    • 容器类型只能和int类型数据进行乘法运算
    • 容器类型进行乘法运算,就是将容器复制指定次数,并进行拼接
# * 效果是什么?
# * 什么容器类型可以使用*

# 基础数据类型  int  float  bool都可以使用*法运算
print(12.1 * 2)

# 容器类型的乘法运算
# 格式: 容器类型 * int类型数据
# 乘法运算的 效果,就是讲容器类型复制指定次数,并拼接到一起

# list 可以使用*法运算么?  可以
list1 = [1, 2, 3]
# 将list1 复制3次并进行拼接
print(list1 * 3)  # [1, 2, 3, 1, 2, 3, 1, 2, 3]
# 使用list 类型乘以float类型可以实现么?
# TypeError: can't multiply sequence by non-int of type 'float'
# 乘法运算不能让容器与非int类型相乘
# print(list1 * 1.3)
# list 能乘以负数么?  可以相乘,但是结果为空列表
print(list1 * -3)  # []
# 可以与0 相乘,结果为空列表
print(list1 * 0)  # []

# tuple 可以使用*法运算么? 可以
tuple1 = (1, 2, 3, 4)
print(tuple1 * 2)  # (1, 2, 3, 4, 1, 2, 3, 4)
# tuple 只能与int类型相乘


# set可以使用 * 法运算么?  不可以
set1 = {1, 2, 3}
# TypeError: unsupported operand type(s) for *: 'set' and 'int'
# 集合类型数据不能做乘法运算
# print(set1 * 3)

# dict 能够使用 * 法运算么?  不可以
dict1 = {'name': 'jack'}
# TypeError: unsupported operand type(s) for *: 'dict' and 'int'
# 字典不能做乘法运算
# print(dict1 * 3)

# 乘号左右两侧的数据可以互换位置么?  可以
print(3 * list1)  # [1, 2, 3, 1, 2, 3, 1, 2, 3]
  • innot in
    • 判断元素是否在数据序列当中
    • 字符串,列表,元组,集合 ,字典都可以使用
# in 判断元素是否存在于容器当中
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}

print(3 in list1)  # True
print(3 in tuple1)  # True
print(3 in set1)  # True
# 如果要判断是否在set当中,要注意被判断的元素必须可以保存在set当中,如果是列表,字典,集合,则不能判断
# print([1, 2] in list1)  # False  可以判断,引为[1,2] 可以保存在list1当中
# print([1, 2] in set1)  # TypeError: unhashable type: 'list' 不能判断,因为[1,2]不能保存到set1当中


# str类型可以使用in么? 可以
str1 = '123'
# TypeError: 'in <string>' requires string as left operand, not int
# 字符串判断时,左侧的元素只能是字符串类型,否则报错
# print(1 in str1)
print('1' in str1)  # True

# dict 是否可以使用in???
dict1 = {'name': 123}
# dict使用in判断的是当前元素是否是dict中存在的键
print(123 in dict1)  # False
print('name' in dict1)  # True

# 如果使用此方法则不能判断字典  列表 集合
# TypeError: unhashable type: 'list'
# print([1,2] in dict1)

# not in  : 所有用法和in相同,只是取反而已

# 结论:
# 1.使用in 和not in  被判断的元素在关键字左侧, 数据序列在关键字右侧
# 2.如果想要判断该元素是否在容器内,该元素必须能保存到容器内,比如集合不能保存列表,字典,集合 所以就不能判断其类型的元素是否在集合内
# 3.字典判断的是元素是否在keys内,也就是是否是其中的键
  • 切片
    • 通过切片按照规则获取数据序列中的一部分元素
    • tuple list str 可以使用切片
    • dict set 不可以使用切片
# 切片:通过切片方法可以按照一定规则截取容器的一部分数据

# str切片
str1 = 'abcde'
# 格式:[起始位置:终止位置:步长]
# 不会修改原有字符串,而是产生了一个新的字符串
print(str1[2:])  # cde

# list可以切片么?
list1 = [1, 2, 3, 4]
# list切片方式方法和str完全相同
# list切片后不会在原有数据上进行修改,而是产生了一个新的列表
print(list1[1:3:1])  # [2, 3]
print(list1)

# tuple 可以切片么?
tuple1 = (1, 2, 3, 4)
# tuple1切片方式方法和str完全相同
# 切片后不会在原有数据上进行修改,而是产生了一个新的元组
print(tuple1[1:3:1])  # (2, 3)
print(tuple1)

# dict可以切片么?  肯定不行,因为不能使用索引获取数据
# set 可以切片么?  肯定不行,因为不能使用索引获取数据

# 结论:
# 1.list str tuple 可以使用切片,格式是:[起始位置:终止位置:步长],三者使用方式完全一致
# 2.所有的切片都不会在原有的数据上进行修改,而是产生一个新的数据序列
# 3.集合和字典无法切片,因为不能使用索引获取数据元素

18、公共函数

  • len :获取容器内元素个数
  • del:删除容器内元素
  • max :获取容器内数据的最大值
  • min : 获取容器内元素的最小值
  • enumerate : 获取容器内元素时可以携带序号
  • range:根据一定规则获取整数序列
# len  获取容器类型的元素个数,  或者说获取容器的长度
str1 = '123'
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}
dict1 = {'name': 123, 'age': 18}
# 使用len可以获取list  str  tuple set中的元素个数
print(len(str1))
print(len(list1))
print(len(tuple1))
print(len(set1))
# 使用len可以获取dict中的键值对的个数
print(len(dict1))

# len() 可以写成  容器.__len__()
print(list1.__len__())

# del
# 删除容器内指定的元素
# list
# del list1[0]
# print(list1)

# tuple
# del tuple1[0]
# TypeError: 'tuple' object doesn't support item deletion
# 元组内元素不能被删除
# print(tuple1)

# set
# for i in set1:
#     del i

# dict
# del dict1['name']
# del 在dict中删除的是键值对
print(dict1)

# str
# TypeError: 'str' object doesn't support item deletion
# str 不能够使用del 删除内部元素
# 注意 :str内部的元素也是不可修改的,类似于元组
# del str1[0]
# print(str1)

# 结论:
# 1.列表,字典可以使用del删除内部元素,但是,列表中是删除元素,字典中是删除键值对
# 2.使用del 没法循环遍历删除set中的元素,因为引用机制问题
# 3.str  tuple内部的元素都不可更改所以不能使用del删除元素


# max  min
# list tuple set str可以使用max  min获取容器内的最大最小值
print(max(list1))
print(max(tuple1))
print(max(set1))
print(max(str1))
# dict是使用max和min获取键的最大最小值
print(max(dict1))

# enumerate  枚举函数:获取容器内数据时添加序号(默认序号从0开始可以作为索引使用)

list2 = [1, 2, 3, 4, 5, 6, 7, 8]

for i in list2:
    print(i)

# 可不可以同时获取元素的值和元素的索引?  可以 使用enumerate

# for i in enumerate(list2):
#     # 直接打印,获取的是以(索引,值)组成的元组
#     print(i)

# list
for index, value in enumerate(list2):
    print(index, value, sep=' : ')

# tuple
for index, value in enumerate(tuple1):
    print(index, value, sep=' : ')

# set
for index, value in enumerate(set1):
    print(index, value, sep=' : ')

# str
for index, value in enumerate(str1):
    print(index, value, sep=' : ')

# dict  
for index, value in enumerate(dict1):
    print(index, value, sep=' : ')
    
# 结论:所有的容器和课迭代类型都可以使用enumerate,并且产生序号,这个序号并不是索引值,而是在生成序号时默认从0开始,碰巧可以在list,str,tuple中当做索引使用

19、推导式

  • 列表推导式
    • 格式:[要插入的值 for 临时变量 in 数据序列 if 条件]
  • 集合推导式
    • 格式:{要插入的值 for 临时变量 in 数据序列 if 条件}
  • 字典推导式
    • 格式:{要插入的键:要插入的值 for 临时变量 in 数据序列 if 条件 }
  • 没有元组推导式和字符串推导式,因为其内部元素无法被修改
# 推导式:通过一定的规则快速构建数据序列
# 列表推导式
# 获取从0 到9的数据序列
# while
list1 = []
i = 0
while i < 10:
    list1.append(i)
    i += 1
print(list1)

# for
list2 = []
for i in range(10):
    list2.append(i)
print(list2)

# 推导式
# 格式: [要插入列表的表达式 for 临时变量 in 数据序列]
list3 = [i for i in range(10)]
print(list3)

# 使用推导式,创建一个从1-100的偶数的数据序列

# for
list4 = []
for i in range(1, 101):
    if i % 2 == 0:
        list4.append(i)

print(list4)

# 推导式
# 格式: [要插入列表的表达式 for 临时变量 in 数据序列 if  条件]
list5 = [i for i in range(1, 101) if i % 2 == 0]
print(list5)

# 练习:
# 用推导式进行九九乘法表的生成,将所有的算式放入列表中
list6 = []
for i in range(1, 10):
    for j in range(1, i + 1):
        list6.append(f'{j} * {i} = {j * i}')

print(list6)

# 改写为推导式:
list7 = [f'{j} * {i} = {j * i}' for i in range(1, 10) for j in range(1, i + 1)]
print(list7)

# 集合推导式
# 集合推导式和列表推导式完全一致,只不过使用推导式时,外层用{}包裹,并且在序列中会去重
set1 = {i for i in range(10)}
print(set1)

# 获取从1-10 的偶数集合
set2 = {i for i in range(1, 11) if i % 2 == 0}
print(set2)

# 字典推导式
keys = ['name', 'age', 'gender', 'id']
values = ['xiaoming', 18, '女', '001']

# 需求想将key 和value以一对应,形成一个字典
dict1 = {}
for i in range(len(keys)):
    dict1[keys[i]] = values[i]

print(dict1)

# 改写推导式
# 格式:{要插入的键:要插入的值  for 临时变量 in 数据序列  if 条件}
dict2 = {keys[i]: values[i] for i in range(len(keys))}
print(dict2)

# 所有的推导式都可以使用for循环改写,所以我们进行推导式的时候先不要急于求成,多改写几次就不用再改写了直接可以写出推导式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值