Python字符串、列表、字典、元组互转
来源:互联网 发布:行知外国语学校好吗 编辑:程序博客网 时间:2024/05/22 12:51
#-*-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}"))
#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}"))
0 0
- Python字符串、列表、字典、元组互转
- Python +字符串+列表+字典
- python 列表字符串字典
- Python字符串,元祖,列表,字典
- python基础(列表,字符串,字典)
- python列表、元组、字典、字符串操作
- Python 列表、元组、字典、字符串
- python基础学习_字符串,列表,字典
- python字符串、列表、字典相互转换
- Python列表;字符串;字典等常用操作
- python 字典列表 列表字典
- Python5--字符串、列表、字典
- Python列表(list)、字典(dict)、字符串(string)基本操作小结
- python基础教程__列表、元组、字符串和字典
- python字符串/元组/列表/字典互转
- python字符串/元组/列表/字典互转
- 初学python 字符串处理、列表的处理、字典的操作
- python字符串/元组/列表/字典互转
- uva 220 Othello
- zookeeper入门之curator框架--原子性操作
- Python之开发环境的搭建
- 使用 Log4j、ActiveMQ 和 Spring 实现异步日志
- 慕课网二次学习(序)
- Python字符串、列表、字典、元组互转
- hibernate级联操作详解
- CMAKE 学习笔记 (二)
- 虚拟视点图像生成012
- python数据分析3:双色球 单个红和蓝球哪个比例高
- 单机版五子棋(JAVA)
- 高效编程之互斥锁和自旋锁
- 二叉搜索树与双向链表
- 第11周补充(2)职员有薪水了!