python字符串/元组/列表/字典互转
2016-01-19 10:50
591 查看
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] - profilers性能分析器
- python的学习资源
- python浅拷贝与深拷贝
- Python算法题----求出和为S的所有连续正整数数列
- Python学习——struct模块的pack、unpack示例
- 配置caffe的python接口及其易错点
- 数学之路-python计算实战(2)-初遇pypy
- Python 5.2 使用@property
- python 内建类型
- python结合API实现即时天气信息
- 3.5!——阶乘的插值(python)
- python matplotlib中文显示问题
- python-整理-面向对象
- python3 crontab 下运行python输出中文失败
- Python中的module,library,package之间的区别
- python结合API实现即时天气信息
- 举例讲解Python面相对象编程中对象的属性与类的方法
- Python学习笔记一
- Python使用os模块和fileinput模块来操作文件目录
- Python编程中的文件读写及相关的文件对象方法讲解