您的位置:首页 > 其它

String(字符串)、List(列表)、Tuple(元组)、Dictionary(字典)转换

2017-11-24 15:38 260 查看
#1、字符串

str1="123"

#字符串转为元组

tuple(eval("(1,2,3)"))

tuple(str1)

#字符串转为列表

list(eval("(1,2,3)"))

tuple(str1)

#字符串转为字典

type(eval("{'name':'ljq', 'age':24}"))

#2、列表

nums=[1, 3, 5, 7, 8, 13, 20]

#列表转为字符串

str(nums)

#列表转为元组

tuple(nums)

#列表不可以转为字典

#3、元组

tup=(1, 2, 3, 4, 5)

#元组转为字符串

tup.__str__()

#元组转为列表,返回:[1, 2, 3, 4, 5]

list(tup)

#元组不可以转为字典

#4、字典

dict = {'name': 'Bob', 'age': 43, '1': 'a'}

#字典转为字符串

type(str(dict)), str(dict)

#字典转为元组('name', 'age', '1')

tuple(dict)

#字典转为元组('Bob', 43, 'a')

print tuple(dict.values())

#字典转为列表['name', 'age', '1']

list(dict)

#字典转为列表['Bob', 43, 'a']

list(dict.values())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐