1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 中list(列表) tupe(元组) str(字符串) dict(字典)之间的相互转换

python 中list(列表) tupe(元组) str(字符串) dict(字典)之间的相互转换

时间:2021-06-08 03:44:07

相关推荐

python 中list(列表) tupe(元组) str(字符串) dict(字典)之间的相互转换

1、字典(dict)

dict = {‘name’: ‘Zara’, ‘age’: 7, ‘class’: ‘First’}

1.1 字典——字符串

返回:

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

1

1.2 字典——元组

返回:(‘age’, ‘name’, ‘class’)

print tuple(dict)

1

1.3 字典——元组

返回:(7, ‘Zara’, ‘First’)

print tuple(dict.values())

1

1.4 字典——列表

返回:[‘age’, ‘name’, ‘class’]

print list(dict)

1

1.5 字典——列表

print dict.values

2、元组

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

2.1 元组——字符串

返回:(1, 2, 3, 4, 5)

print tup.__str__()

1

2.2 元组——列表

返回:[1, 2, 3, 4, 5]

print list(tup)

1

2.3 元组不可以转为字典

3、列表

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

3.1 列表——字符串

返回:[1, 3, 5, 7, 8, 13, 20]

print str(nums)

1

3.2 列表——元组

返回:(1, 3, 5, 7, 8, 13, 20)

print tuple(nums)

1

3.3 列表不可以转为字典

4、字符串

4.1 字符串——元组

返回:(1, 2, 3)

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

1

4.2 字符串——列表

返回:[1, 2, 3]

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

1

4.3 字符串——字典

返回:

print type(eval("{'name':'ljq', 'age':24}"))转自 /violet_echo_0908/article/details/52486689

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。