码迷,mamicode.com
首页 > 其他好文 > 详细

eval函数的用法

时间:2017-08-09 00:06:45      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:span   print   eva   函数   nbsp   str   blog   列表   字典   

 1 可以把list,tuple,dict和string相互转化。
 2 #################################################
 3 字符串转换成列表
 4 >>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
 5 >>>type(a)
 6 <type str>
 7 >>> b = eval(a)
 8 >>> print b
 9 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
10 >>> type(b)
11 <type list>
12 #################################################
13 字符串转换成字典
14 >>> a = "{1: ‘a‘, 2: ‘b‘}"
15 >>> type(a)
16 <type str>
17 >>> b = eval(a)
18 >>> print b
19 {1: a, 2: b}
20 >>> type(b)
21 <type dict>
22 #################################################
23 字符串转换成元组
24 >>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
25 >>> type(a)
26 <type str>
27 >>> b = eval(a)
28 >>> print b
29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
30 >>> type(b)
31 <type tuple>

 

eval函数的用法

标签:span   print   eva   函数   nbsp   str   blog   列表   字典   

原文地址:http://www.cnblogs.com/52-qq/p/7309492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!