Python eval 函数妙用(转)
来源:互联网 发布:联想有windows系统平板 编辑:程序博客网 时间:2024/06/03 15:39
作者博文地址:https://www.cnblogs.com/liu-shuai/
eval
功能:将字符串str当成有效的表达式来求值并返回计算结果。
语法: eval(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 = 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 b19 {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 b29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))30 >>> type(b)31 <type 'tuple'>
阅读全文
0 0
- Python eval 函数妙用(转)
- Python eval 函数妙用
- Python eval 函数妙用
- Python eval 函数妙用
- Python eval 函数妙用
- Python eval 函数妙用
- python eval()函数的妙用和滥用
- python中eval()的妙用
- Python:eval的妙用和滥用
- Python:eval的妙用和滥用
- Python:eval的妙用和滥用
- Python:eval的妙用和滥用
- Python:eval的妙用和滥用
- eval 妙用
- python神奇函数之eval()学习
- python eval函数
- Python eval()函数
- python中的eval函数
- 微信分布式数据存储协议对比——Paxos和Quorum
- 13.7 SpringBoot集成日志系统logback的几个问题
- 13.8 java.lang.IllegalArgumentException: Request header is too large
- the suspect
- 13.10 Scala中使用JSON.toJSONString报错:ambiguous reference to overloaded definition
- Python eval 函数妙用(转)
- 13.9 SpringBoot集成Swagger2中遇到的问题
- 13.4 库依赖冲突问题:Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path
- 润乾报表v5如何升级设计器的JDK版本
- 13.11 Scala混用Java的集合类调用scala的foreach遍历问题
- OSG的几个矩阵
- 9月12日云栖精选夜读:人工智能在细分及新兴领域的应用
- 13.12 Spring Boot集成Security中遇到的问题
- 13.13 java.util.ConcurrentModificationException