设为首页 加入收藏

TOP

【转】eva l()函数用法
2017-10-09 16:53:50 】 浏览:839
Tags:eva 函数 用法

eva l

  功能:将字符串str当成有效的表达式来求值并返回计算结果。

  语法: eva l(source[, globals[, locals]]) -> value

  参数:

    source:一个Python表达式或函数compile()返回的代码对象

    globals:可选。必须是dictionary

    locals:可选。任意map对象

  实例展示:

 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 = eva l(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 = eva l(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 = eva l(a)
28 >>> print b
29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
30 >>> type(b)
31 <type 'tuple'>

参考地址:http://www.cnblogs.com/liu-shuai/p/6098246.html

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python入门:条件控制 下一篇python学习——python介绍

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目