7.容器类型数据转换

# 容器类型数据转换

# list列表
'''
字符串转换为列表时 会把字符串中的每一个字符当做列表的元素
数字类型是 非容器类型,不能转换为列表
集合 可以转换为 list列表类型
元组 可以转换为list类型
字典 可以转换为list类型,只保留了字典中的键
'''
# n =(1,3)
# res =list(n)
# print(n,type(n),res,type(res))

# tuple 元组
'''
数字类型 非容器类型, 不能转换为元组
其他容器类型的数据进行转换时和列表一样
'''
# n =(1,3)
# res =tuple(n)
# print(n,type(n),res,type(res))

# set 集合
'''
数字类型 非容器类型 不能转换为集合
字符串,列表,元组 可以转换为集合 结果是无序的
字典转换为集合时只保留了键 key

'''
# n =(1,3)
# res =set(n)
# print(n,type(n),res,type(res))

# 字典
'''
数字类型 非容器类型 不能转换为字典
字符串 不能直接转换为字典

列表可以转换为字典,要求是一个二级列表,并且每个二级元素只有两个值
元组可以转换为字典,要求是一个二级列表,并且每个二级元素只有两个值

'''
n =(1,3)
res =dict(n)
print(n,type(n),res,type(res))

猜你喜欢

转载自blog.csdn.net/m0_60413225/article/details/121460828