1. 程式人生 > >python中eval函式作用

python中eval函式作用

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(type(b))
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)