一、字符串(str)
字符串转换为列表
使用list()方法
str_1 = "1235" str_2 = 'zhangsan' str_3 = '''lisi''' tuple_1 = list(str_1) tuple_2 = list(str_2) tuple_3 = list(str_3) print(type(tuple_1)) print(type(tuple_2)) print(type(tuple_3)) print(tuple_1) print(tuple_2) print(tuple_3)
运行结果:
使用Python中字符串的内置方法split()
Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
语法:str.split(str="", num=string.count(str)).
①str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
②num – 分割次数。默认为 -1, 即分隔所有。
str_1 = "12 35 213" str_2 = 'zhang san shi a ' str_3 = 'zhang san shi a ' str_4 = '''li si wang wu''' list_1 = str_1.split(" ") list_2 = str_2.split(" ",1) list_3 = str_3.split(" ") list_4 = str_4.split(" ",2) print(type(list_1)) print(type(list_2)) print(type(list_3)) print(type(list_4)) print(list_1) print(list_2) print(list_3) print(list_4)
运行结果:
字符串 转换为 元组
使用tuple()方法
str_1 = "1235" str_2 = 'zhangsan' str_3 = '''lisi''' list_1 = tuple(str_1) list_2 = tuple(str_2) list_3 = tuple(str_3) print(type(list_1)) print(type(list_2)) print(type(list_3)) print(list_1) print(list_2) print(list_3)
运行结果:
字符串 转换为 字典
利用eval()方法,可以将字典格式的字符串转换为字典
eval() 函数用来执行一个字符串表达式,并返回表达式的值。
语法:eval(expression[, globals[, locals]])
①expression – 表达式。
②globals – 变量作用域,全局命名空间,如果被提供,则必须是一个字典对象。③locals – 变量作用域,局部命名空间,如果被提供,可以是任何映射对象。
str_1 = "{'name':'zhangsan','age':14,'gender':'girl'}" dict_1 = eval(str_1) print(type(dict_1)) print(dict_1)
运行结果:
利用json.loads()方法,可以将字典格式的字符串转换为字典
json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。
语法:json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
import json str_1 = '{"name":"xiaoming","age":18}' dict_1 = json.loads(str_1) print(type(dict_1)) print(dict_1)
运行结果:
二、列表(list)
列表转字符串
利用‘'.join()将列表中的内容拼接程一个字符串
Python join() 方法用于将序列中的元素(必须是str) 以指定的字符(''中指定的) 连接生成一个新的字符串。
list_1 = ['a', 'b', 'c'] str_1 = ''.join(list_1) print(type(str_1)) print(str_1)
运行结果:
列表转字典
利用for in rang将两个列表转换为字典
list_1 = ['a', 'b', 'c'] list_2 = [1, 2, 3] dict_1 = {} for i in range(len(list_1)): dict_1[list_1[i]] = list_2[i] print(type(dict_1)) print(dict_1)
运行结果:
利用python内置方法dict()和zip()将两个列表转换为字典
dict() 函数用于创建一个字典。
语法:class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, kwarg)
①kwargs – 关键字
②mapping – 元素的容器。
③iterable – 可迭代对象。
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
语法:zip([iterable, …])
iterabl – 一个或多个迭代器;
list_1 = ['name', 'age'] list_2 = ['zhangsan',18] dict_1 = dict(zip(list_1, list_2)) print(type(dict_1)) print(dict_1)
运行结果:
三、元组(tuple)
元组转换为字符串
- 使用方法__str__
- 返回一个对象的描述信息
tuple_1 = (1, 2, 3) str_1 = tuple_1.__str__() print(type(str_1)) print(str_1)
运行结果:
元组转换为列表使用方法list()
list() 方法用于将元组转换为列表。
语法:list( tup )
tup – 要转换为列表的元组。
tuple_1 = (1, 2, 3) list_1 = list(tuple_1) print(type(list_1)) print(list_1)
运行结果:
元组不能转换为字典
四、字典(dict)
字典转换为字符串
使用 json.dumps()方法
json.dumps 用于将 Python 对象编码成 JSON 字符串。
json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding=“utf-8”, default=None, sort_keys=False, **kw)
字典转换为元组
- 使用方法 tuple()
- 字典在转换为元组之后,只会保存关键字
dict_1 = {"name":"zhangsan", "age":18} tuple_1 = tuple(dict_1) print(type(tuple_1)) print(tuple_1)
运行结果:
字典转换为列表
- 使用方法 list()
- 字典在转换为列表之后,只会保存关键字
dict_1 = {"name":"zhangsan", "age":18} list_1 = list(dict_1) print(type(list_1)) print(list_1)
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]