Python中如何实现字符串、元组、列表、字典间的相互转换示例解析?

2026-06-09 18:034阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计571个文字,预计阅读时间需要3分钟。

Python中如何实现字符串、元组、列表、字典间的相互转换示例解析?

Python 中字符串、元组、列表和字典之间的相互转换可以直接使用内置函数完成。以下是一些简单的例子:

1. 字符串转列表:pythons=hellol=list(s)

2. 列表转字符串:pythonl=[104, 101, 108, 108, 111]s=''.join(chr(i) for i in l)

3. 字符串转元组:pythons=pythont=tuple(s)

4. 元组转字符串:pythont=(80, 121, 116, 104, 111, 110, 103)s=''.join(chr(i) for i in t)

5. 列表转字典(假设列表是键值对):pythonkeys=[a, b, c]values=[1, 2, 3]d=dict(zip(keys, values))

6. 字典转列表(键值对):pythond={a: 1, b: 2, c: 3}keys=list(d.keys())values=list(d.values())

7. 字典转元组(键值对):pythond={a: 1, b: 2, c: 3}t=list(d.items())

8. 元组转字典:pythont=[(a, 1), (b, 2), (c, 3)]d=dict(t)

python字符串,元组,列表,字典互相转换直接给大家上代码实例

#-*-coding:utf-8-*- #1、字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type 'str'> {'age': 7, 'name': 'Zara', 'class': 'First'} print type(str(dict)), str(dict) #字典可以转为元组,返回:('age', 'name', 'class') print tuple(dict) #字典可以转为元组,返回:(7, 'Zara', 'First') print tuple(dict.values()) #字典转为列表,返回:['age', 'name', 'class'] print list(dict) #字典转为列表 print dict.values #2、元组 tup=(1, 2, 3, 4, 5) #元组转为字符串,返回:(1, 2, 3, 4, 5) print tup.__str__() #元组转为列表,返回:[1, 2, 3, 4, 5] print list(tup) #元组不可以转为字典 #3、列表 nums=[1, 3, 5, 7, 8, 13, 20]; #列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20] print str(nums) #列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20) print tuple(nums) #列表不可以转为字典 #4、字符串 #字符串转为元组,返回:(1, 2, 3) print tuple(eval("(1,2,3)")) #字符串转为列表,返回:[1, 2, 3] print list(eval("(1,2,3)")) #字符串转为字典,返回:<type 'dict'> print type(eval("{'name':'ljq', 'age':24}"))

更多关于python字符串,元组,列表,字典互转的代码请查看下面的相关链接

Python中如何实现字符串、元组、列表、字典间的相互转换示例解析?

本文共计571个文字,预计阅读时间需要3分钟。

Python中如何实现字符串、元组、列表、字典间的相互转换示例解析?

Python 中字符串、元组、列表和字典之间的相互转换可以直接使用内置函数完成。以下是一些简单的例子:

1. 字符串转列表:pythons=hellol=list(s)

2. 列表转字符串:pythonl=[104, 101, 108, 108, 111]s=''.join(chr(i) for i in l)

3. 字符串转元组:pythons=pythont=tuple(s)

4. 元组转字符串:pythont=(80, 121, 116, 104, 111, 110, 103)s=''.join(chr(i) for i in t)

5. 列表转字典(假设列表是键值对):pythonkeys=[a, b, c]values=[1, 2, 3]d=dict(zip(keys, values))

6. 字典转列表(键值对):pythond={a: 1, b: 2, c: 3}keys=list(d.keys())values=list(d.values())

7. 字典转元组(键值对):pythond={a: 1, b: 2, c: 3}t=list(d.items())

8. 元组转字典:pythont=[(a, 1), (b, 2), (c, 3)]d=dict(t)

python字符串,元组,列表,字典互相转换直接给大家上代码实例

#-*-coding:utf-8-*- #1、字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type 'str'> {'age': 7, 'name': 'Zara', 'class': 'First'} print type(str(dict)), str(dict) #字典可以转为元组,返回:('age', 'name', 'class') print tuple(dict) #字典可以转为元组,返回:(7, 'Zara', 'First') print tuple(dict.values()) #字典转为列表,返回:['age', 'name', 'class'] print list(dict) #字典转为列表 print dict.values #2、元组 tup=(1, 2, 3, 4, 5) #元组转为字符串,返回:(1, 2, 3, 4, 5) print tup.__str__() #元组转为列表,返回:[1, 2, 3, 4, 5] print list(tup) #元组不可以转为字典 #3、列表 nums=[1, 3, 5, 7, 8, 13, 20]; #列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20] print str(nums) #列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20) print tuple(nums) #列表不可以转为字典 #4、字符串 #字符串转为元组,返回:(1, 2, 3) print tuple(eval("(1,2,3)")) #字符串转为列表,返回:[1, 2, 3] print list(eval("(1,2,3)")) #字符串转为字典,返回:<type 'dict'> print type(eval("{'name':'ljq', 'age':24}"))

更多关于python字符串,元组,列表,字典互转的代码请查看下面的相关链接

Python中如何实现字符串、元组、列表、字典间的相互转换示例解析?