支持转换为 list 的类型,只能是序列,比如:str、tuple、dict、set等
str -> list:将字符串转换成列表
# str -> list:将字符串转换成列表
str1 = 'hello, 同学们'
print(type(str1)) # 类型检测
list1 = list(str1) # 类型转换
print(type(list1)) # 类型检测
print(list1)
bytes -> list:将字节串转换成列表
# bytes -> list:将字节串转换成列表
bytes1 = b'hello'
print(type(bytes1)) # 类型检测
list1 = list(bytes1) # 类型转换
print(type(list1)) # 类型检测
print(list1)
tuple -> list:将元组转换成列表
# tuple -> list:将元组转换成列表
tuple1 = ('h', 3, '23', 0)
print(type(tuple1)) # 类型检测
list1 = list(tuple1) # 类型转换
print(type(list1)) # 类型检测
print(list1)
dict -> list:将字典转换成列表,会取键名作为列表的值
# dict -> list:将字典转换成列表,会取键名作为列表的值
dict1 = {'name': 'xiaoming', 'age': 32, 'weight': '保密'}
print(type(dict1)) # 类型检测
list1 = list(dict1) # 类型转换
print(type(list1)) # 类型检测
print(list1)
set -> list:将集合转换成列表,会先去除重复的集合数值
# set -> list:将集合转换成列表,会先去除重复的集合数值,排好序之后再转换
set1 = {3, 3, 2, 2, 1, '好', '好'}
print(type(set1)) # 类型检测
list1 = list(set1) # 类型转换
print(type(list1)) # 类型检测
print(list1)
与列表一样,支持转换为 tuple 的类型,只能是序列、
str -> tuple:将字符串转换成元组
# str -> tuple:将字符串转换成列表
str1 = 'hello, 同学们'
print(type(str1)) # 类型检测
tuple1 = tuple(str1) # 类型转换
print(type(tuple1)) # 类型检测
print(tuple1)
bytes -> tuple:将字节串转换成元组
# bytes -> tuple:将字节串转换成元组
bytes1 = b'hello'
print(type(bytes1)) # 类型检测
tuple1 = tuple(bytes1) # 类型转换
print(type(tuple1)) # 类型检测
print(tuple1)
dict -> tuple:将字典转换成元组,会取键名作为元组的值
# dict -> tuple:将字典转换成元组,会取键名作为元组的值
dict1 = {'name': 'xiaoming', 'age': 32, 'weight': '保密'}
print(type(dict1)) # 类型检测
tuple1 = tuple(dict1) # 类型转换
print(type(tuple1)) # 类型检测
print(tuple1)
set -> tuple:将集合转换成元组,会先去除重复的集合数值
# set -> tuple:将集合转换成元组,会先去除重复的集合数值,排好序之后再转换
set1 = {3, 3, 2, 2, 1, '好', '好'}
print(type(set1)) # 类型检测
tuple1 = tuple(set1) # 类型转换
print(type(tuple1)) # 类型检测
print(tuple1)
list -> tuple:将列表转换成元组
# list -> tuple:将列表转换成元组
list1 = ['h', 3, '23', 0]
print(type(list1)) # 类型检测
tuple1 = tuple(list1) # 类型转换
print(type(tuple1)) # 类型检测
print(tuple1)
str -> set:将字符串转换成集合,先将字符切割成元组,然后再去转换为集合
# str -> set:将字符串转换成集合
str1 = 'hello, 同学们'
print(type(str1)) # 类型检测
set1 = set(str1) # 类型转换
print(type(set1)) # 类型检测
print(set1)
bytes -> set:将字节串转换成集合,会取每个字节的 ASCII 十进制值并组合成元组,然后再去转换为集合
# bytes -> set:将字节串转换成集合
bytes1 = b'hello'
print(type(bytes1)) # 类型检测
set1 = set(bytes1) # 类型转换
print(type(set1)) # 类型检测
print(set1)
list -> set:将列表转换成集合,先对列表去重,再转换
# list -> set:将列表转换成集合
list1 = ['h', 3, 0, '23', 0]
print(type(list1)) # 类型检测
set1 = set(list1) # 类型转换
print(type(set)) # 类型检测
print(set1)
tuple -> set:将元组转换成集合,先对元组去重,再转换
# tuple -> set:将元组转换成集合,先对元组去重,再转换
tuple1 = ('h', 3, '23', 0, 0)
print(type(tuple1)) # 类型检测
set1 = set(tuple1) # 类型转换
print(type(set1)) # 类型检测
print(set1)
dict -> set:将字典转换成集合,会取字典的键名组合成集合
# dict -> set:将字典转换成集合,会取键名作为集合的值
dict1 = {'name': 'xiaoming', 'age': 32, 'weight': '保密'}
print(type(dict1)) # 类型检测
set1 = set(dict1) # 类型转换
print(type(set1)) # 类型检测
print(set1)
str -> dict:将字符串转换成字典,使用 ast.literal_eval 方法
# str -> dict:将字符串转换成字典,使用 ast.literal_eval 方法
import ast # 导入模块
user_info = "{'name': 'john', 'gender': 'male', 'age': 28}"
user_dict = ast.literal_eval(user_info)
print(user_dict)
list -> dict:将列表转换成字典
将列表转换成字典,通过 zip 将 2 个列表映射为字典
将嵌套的列表转换为字典
注意:元组转换成字典与此类似
# list -> dict:将列表转换成字典,通过 zip 将 2 个列表映射为字典
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3]
print(dict(zip(list1, list2)))
# 将嵌套的列表转换为字典
list1 = [
[1, 111],
[2, 222],
[3, 333],
]
print(dict(list1))
set -> dict:将集合转换成字典(字典的值是随机的),通过 zip 将 2 个集合映射为字典
# set -> dict:将集合转换成字典,通过 zip 将 2 个集合映射为字典
set1 = {1, 2, 3}
set2 = {'a', 'b', 'c'}
print(dict(zip(set1, set2)))
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!