python中数组,元组,字典和字符串之间的转换,,1、字典字典转为字符


1、字典

字典转为字符串

>>>dict={‘name‘:‘zhzhgo‘,‘age‘:25}>>>printtype(str(dict)),str(dict)<type‘str‘>{‘age‘:25,‘name‘:‘zhzhgo‘}>>>

字典转为元组

>>>dict={‘name‘:‘zhzhgo‘,‘age‘:25}>>>printtype(tuple(dict)),tuple(dict)<type‘tuple‘>(‘age‘,‘name‘)>>>

字典转为列表

>>>dict={‘name‘:‘zhzhgo‘,‘age‘:25}>>>printtype(list(dict)),list(dict)<type‘list‘>[‘age‘,‘name‘]>>>


2、元组

元组转为字符串

>>>mytupe=(1,2,3)>>>printmytupe.__str__()(1,2,3)>>>>>>mylist=(‘h‘,‘e‘,‘l‘,‘l‘,‘o‘)>>>print‘‘.join(mylist)hello>>>

元组转为列表

>>>mytupe=(1,2,3)>>>printlist(mytupe)[1,2,3]>>>

元组不可以转为字典


3、列表

列表转为字符串

>>>mylist=[‘h‘,‘e‘,‘l‘,‘l‘,‘o‘]>>>print‘‘.join(mylist)hello>>>


列表转为元组

>>>mylist=[1,2,3]>>>printtuple(mylist)(1,2,3)>>>


列表不可以转为字典


4、字符串

字符串转为元组

>>>mystring="hello">>>printtuple(mystring)(‘h‘,‘e‘,‘l‘,‘l‘,‘o‘)>>>

字符串转为列表

>>>mystring="hello">>>printlist(mystring)[‘h‘,‘e‘,‘l‘,‘l‘,‘o‘]>>>

字符串转为字典

>>>mystring="{‘name‘:‘zhzhgo‘,‘age‘:25}">>>printtype(eval(mystring))<type‘dict‘>>>>

本文出自 “今日的努力,明日的成功!” 博客,请务必保留此出处http://zhzhgo.blog.51cto.com/10497096/1671868

python中数组,元组,字典和字符串之间的转换

评论关闭