Python四种数据类型[str/list/tuple/dict]之间的转换

您所在的位置:网站首页 list转int Python四种数据类型[str/list/tuple/dict]之间的转换

Python四种数据类型[str/list/tuple/dict]之间的转换

2024-07-15 06:00| 来源: 网络整理| 查看: 265

一、字符串[str]转其他类型 1.1、字符串[str]转列表[list]

直接方法:用list()内置函数

str_0 = "abc" list_1 = list(str_0) print("字符串转列表:", list_1, type(list_1)) # 结果:由 "abc"——>>['a', 'b', 'c'] 1.2、字符串[str]转元组[tuple]

曲线救国:由字符串先转为列表再转为元组

str_0 = "abc" list_1 = list(str_0) tuple_now = tuple(list_1) print("字符串转元组:", tuple_now, type(tuple_now)) # 结果:由 "abc"——>>('a', 'b', 'c') 1.3、字符串[str]转字典[dict]

直接方法:用eval()内置函数,要求字符串必须保持字典的样式

str_0 = "{'aaa':1, 'bbb':2, 'ccc':3}" print(type(str_0)) dict_1 = eval(str_0) print("字符串转字典:", dict_1, type(dict_1))

–-----------------------------------------------------------------------------—--------------------------------------------------------—---- –-----------------------------------------------------------------------------—--------------------------------------------------------—----

二、列表[list]转其他类型 2.1、列表[list]转字符串[str]

方法一:join()函数

list_0 = ["h", "i", "j"] str_1 = "".join(list_0) # str_1 = ",".join(list_0) print("列表转字符串1:", str_1, type(str_1)) # 结果:由 ["h", "i", "j"]——>>hij

方法二:str()内置函数

list_0 = ["h", "i", "j"] str_1 = str(list_0) print("列表转字符串2:", str_1, type(str_1)) # 结果:由 ["h", "i", "j"]——>>['h', 'i', 'j'] 2.2、列表[list]转元组[tuple]

直接方法:tuple()内置函数

list_0 = ["h", "i", "j"] tuple_1 = tuple(list_0) print("列表转元组:", tuple_1, type(tuple_1)) # 结果:由 ["h", "i", "j"]——>>('h', 'i', 'j') 2.3、单个列表不能直接转字典

原因:字典是由“键值对”构成,单个列表只能充当“键”或者“值” –-----------------------------------------------------------------------------—--------------------------------------------------------—---- –-----------------------------------------------------------------------------—--------------------------------------------------------—----

三、元组[tuple]转其他类型 3.1、元组[tuple]转字符串[str]

方法一:join()函数

tuple_0 = ("d", "e", "f") str_1 = "".join(tuple_0) # str_1 = ",".join(tuple_0) print("元组转字符串1:", str_1, type(str_1)) # 结果:由 ("d", "e", "f")——>>def

方法二:内置函数

tuple_0 = ("d", "e", "f") str_1 = tuple_0.__str__() print("元组转字符串2:", str_1, type(str_1)) # 结果:由 ("d", "e", "f")——>>('d', 'e', 'f') 3.2、元组[tuple]转列表[list]

直接方法:list()函数

tuple_0 = ("d", "e", "f") list_1 = list(tuple_0) print("元组转列表:", list_1, type(list_1)) # 结果:由 ("d", "e", "f")——>>['d', 'e', 'f'] 3.3、单个元组不能直接转字典

原因:字典是由“键值对”构成,单个元组只能充当“键”或者“值” –-----------------------------------------------------------------------------—--------------------------------------------------------—---- –-----------------------------------------------------------------------------—--------------------------------------------------------—----

四、字典[dict]转其他类型 4.1、字典[dict]转字符串[str]

直接方法:str()内置函数

dict_0 = {"name":"zcm", "age":24} str_1 = str(dict_0) print("字典转字符串:", str_1, type(str_1)) # 结果:由 {"name":"zcm", "age":24}——>>{'name': 'zcm', 'age': 24} 4.2、字典[dict]转列表[list]

说明:只能将字典的“key”或者“value”单独拿出来转成列表,用list()函数

# 4.2:字典的key转列表 dict_0 = {"name":"zcm", "age":24} list_key = list(dict_0) print("字典的key转列表:", list_key, type(list_key)) # 结果:由 {"name":"zcm", "age":24}——>>['name', 'age'] # 4.2:字典的value转列表 dict_0 = {"name":"zcm", "age":24} list_value = list(dict_0.values()) print("字典的value转列表:", list_value, type(list_value)) # 结果:由 {"name":"zcm", "age":24}——>>['zcm', 24] 4.3、字典[dict]转元组[tuple]

说明:只能将字典的“key”或者“value”单独拿出来转成元组,用tuple()函数

# 4.3:字典的key转元组 dict_0 = {"name":"zcm", "age":24} tuple_key = tuple(dict_0) print("字典的key转元组:", tuple_key, type(tuple_key)) # 结果:由 {"name":"zcm", "age":24}——>>('name', 'age') # 4.3:字典的value转元组 dict_0 = {"name":"zcm", "age":24} tuple_value = tuple(dict_0.values()) print("字典的value转元组:", tuple_value, type(tuple_value)) # 结果:由 {"name":"zcm", "age":24}——>>('zcm', 24)

–-----------------------------------------------------------------------------—--------------------------------------------------------—---- –-----------------------------------------------------------------------------—--------------------------------------------------------—---- –-----------------------------------------------------------------------------—--------------------------------------------------------—----

完整的代码如下: # -*- coding:utf-8 -*- # -*- author:zzZ_CMing CSDN address:https://blog.csdn.net/zzZ_CMing # -*- 2018/12/02; 14:52 # -*- python3.6 """ # python中不同的数据类型有不同的使用场景,只有使用符合规定的数据类型才会得到正确结果, # 所以数据类型中转换是非常重要的 # 字符串(str),元组(tuple),列表(list),字典(dict)之间的转化。 """ import json str_0 = "abc" tuple_0 = ("d", "e", "f") list_0 = ["h", "i", "j"] dict_0 = {"name":"zcm", "age":24} #---------------------------------- # 1、字符串(str)转其他类型 #---------------------------------- # 1.1:字符串转元组:由字符串先转为列表再转为元组 str_0 = "abc" list_1 = list(str_0) print("11111", list_1, type(list_1)) tuple_now = tuple(list_1) print("字符串转元组:", tuple_now, type(tuple_now)) # 1.2:字符串转列表:list()内置函数 str_0 = "abc" list_1 = list(str_0) print("字符串转列表:", list_1, type(list_1)) # 1.3:字符串转字典:eval()内置函数 # 重要 str_0 = "{'aaa':1, 'bbb':2, 'ccc':3}" print(type(str_0)) dict_1 = eval(str_0) print("字符串转字典:", dict_1, type(dict_1)) print("\n") #---------------------------------- # 2、元组(tuple)转其他类型 #---------------------------------- # 2.1:元组转字符串:方法一:join()函数 # 重要 tuple_0 = ("d", "e", "f") str_1 = "".join(tuple_0) # str_1 = ",".join(tuple_0) print("元组转字符串1:", str_1, type(str_1)) # 2.1:元组转字符串:方法二:内置函数 tuple_0 = ("d", "e", "f") str_1 = tuple_0.__str__() print("元组转字符串2:", str_1, type(str_1)) # 2.2:元组转列表:list()函数 tuple_0 = ("d", "e", "f") list_1 = list(tuple_0) print("元组转列表:", list_1, type(list_1)) # 2.3:元组转字典: tuple_key = () tuple_value = () print("元组不可直接转为字典") print("\n") #---------------------------------- # 3、列表(list)转其他类型 #---------------------------------- # 3.1:列表转字符串:方法一:join()函数 list_0 = ["h", "i", "j"] str_1 = "".join(list_0) print("列表转字符串1:", str_1, type(str_1)) # 3.1:列表转字符串:方法二:str()内置函数 list_0 = ["h", "i", "j"] str_1 = str(list_0) print("列表转字符串2:", str_1, type(str_1)) # 3.2:列表转元组:tuple()内置函数 list_0 = ["h", "i", "j"] tuple_1 = tuple(list_0) print("列表转元组:", tuple_1, type(tuple_1)) # 3.3:列表转字典: list_key = [] list_value = [] print("列表不可直接转为字典") print("\n") #---------------------------------- # 4、字典(dict)转其他类型 #---------------------------------- # 4.1:字典转字符串:str()内置函数 dict_0 = {"name":"zcm", "age":24} str_1 = str(dict_0) print("字典转字符串:", str_1, type(str_1)) # 4.2:字典的key转元组 dict_0 = {"name":"zcm", "age":24} tuple_key = tuple(dict_0) print("字典的key转元组:", tuple_key, type(tuple_key)) # 4.2:字典的value转元组 dict_0 = {"name":"zcm", "age":24} tuple_value = tuple(dict_0.values()) print("字典的value转元组:", tuple_value, type(tuple_value)) # 4.3:字典的key转列表 dict_0 = {"name":"zcm", "age":24} list_key = list(dict_0) print("字典的key转列表:", list_key, type(list_key)) # 4.3:字典的value转列表 dict_0 = {"name":"zcm", "age":24} list_value = list(dict_0.values()) print("字典的value转列表:", list_value, type(list_value))


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3