python字串 元組 列表 字典互轉

2021-07-03 12:08:48 字數 936 閱讀 2901

#-*-coding:utf-8-*- 

#1、字典

dict =

#字典轉為字串,返回:

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)"))

#字串轉為字典,返回:

print type(eval(""))

python字串 元組 列表 字典互轉

coding utf 8 1 字典 dict 字典轉為字串,返回 print type str dict str dict 字典可以轉為元組,返回 age name class print tuple dict 字典可以轉為元組,返回 7,zara first print tuple dict.va...

python字串 元組 列表 字典互轉

coding utf 8 1 字典 dict 字典轉為字串,返回 print type str dict str dict 字典可以轉為元組,返回 age name class print tuple dict 字典可以轉為元組,返回 7,zara first print tuple dict.va...

python 字串 元組 列表 字典互轉

1 字典 dict 字典轉為字串,返回 print type str dict str dict 字典可以轉為元組,返回 age name class print tuple dict 字典可以轉為元組,返回 7,zara first print tuple dict.values 字典轉為列表,返...