python之eval函数

转自(https://www.cnblogs.com/niuniu2018/p/7737556.html)
eval函数就是实现list、dict、tuple与str之间的转化
str函数把list,dict,tuple转为为字符串

字符串转换成列表

a = “[[1,2], [3,4], [5,6], [7,8], [9,0]]”
print(type(a))
b = eval(a)
print(b)

字符串转换成字典

a = “{1: ‘a’, 2: ‘b’}”
print(type(a))
b = eval(a)
print(type(b))
print(b)

字符串转换成元组

a = “([1,2], [3,4], [5,6], [7,8], (9,0))”
print(type(a))
b=eval(a)
print(type(b))
print(b)

猜你喜欢

转载自blog.csdn.net/helloworld0906/article/details/82954019