python中的eval函数使用实例
作者:菜鸟更要努力呀 时间:2021-06-20 00:34:07
eval()函数常被称为评估函数,它的功能是去掉参数最外侧引号,变成python可执行的语句,并执行语句的函数。
1 eval函数的简介和语法:
eval()函数用来执行一个字符串表达式,并返回表达式的值。还可以把字符串转化为list、tuple、dict。
eval函数的语法:
eval(expression[,globals[,locals]])
参数:
expression:表达式。
globals:变量作用域,如果被提供,必须是一个字典对象。
locals:变量作用域,如果被提供,可以说任何映射对象。
2 eval()函数使用实例:
1 字符串转换成列表:
a="[1,2,3,4,5]"
b=eval(a)
# a是字符串类型数据,b是列表类型数据
2 字符串转换成字典 :
a="{"name":"guo","age":25}"
b=eval(a)
# a为字符串类型数据,b为字典类型数据
3 字符串转换为元组:
a="(1,2,3,4,5)"
eval(a)
# a的数据结构是字符串 b的数据结构是元组
4 eval()返回表达式的值:
x=4
eval("3*x")
# 返回值为12
x=10
g={'a':4}
eval{"a+1",g}
# 返回值为 5
x=10
b=20
c=30
g={'a':6,'b':8}
t={'b':100,'c':10}
eval('a+b+c',g,t)
# 返回值为116
来源:https://blog.csdn.net/quanlingtu1272/article/details/95454722
标签:python,eval,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现迪杰斯特拉算法过程解析
2022-08-14 09:55:42
Ubuntu下使用python读取doc和docx文档的内容方法
2021-04-09 03:42:30
python子类在多继承中使用MRO机制原理
2021-03-09 00:52:35
归纳万恶IE6的HACK方法
2010-02-04 17:17:00
Asp中通过简单的例子理解下ByVal和ByRef的用法
2011-02-20 10:57:00
python解析xml文件方式(解析、更新、写入)
2022-03-07 05:19:01
![](https://img.aspxhome.com/file/2023/3/68853_0s.jpg)
网页绿色系配色应用实例
2008-08-26 11:51:00
![](https://img.aspxhome.com/file/UploadPic/20088/26/2008826115322660s.gif)
python处理圆角图片、圆形图片的例子
2021-06-08 15:20:15
![](https://img.aspxhome.com/file/2023/6/92356_0s.png)
asp删除mssql数据库中没有记录的图片代码
2011-03-11 11:22:00
一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
2023-01-09 19:50:16
![](https://img.aspxhome.com/file/2023/3/79623_0s.jpg)
如何通过命令行进入python
2022-10-28 22:12:56
![](https://img.aspxhome.com/file/2023/9/79529_0s.png)
sql存储过程获取汉字拼音头字母函数
2011-11-03 16:58:05
Python当中的array数组对象实例详解
2022-02-12 23:22:47
![](https://img.aspxhome.com/file/2023/8/81708_0s.png)
python创建学生成绩管理系统
2023-08-09 04:19:38
![](https://img.aspxhome.com/file/2023/9/62299_0s.jpg)
使用torchtext导入NLP数据集的操作
2023-08-19 16:07:10
![](https://img.aspxhome.com/file/2023/6/85276_0s.jpg)
python 名称空间与作用域详情
2021-06-10 23:12:38
![](https://img.aspxhome.com/file/2023/8/71548_0s.png)
Python3列表删除的三种方式实现
2021-03-26 17:55:55
![](https://img.aspxhome.com/file/2023/5/89305_0s.jpg)
Python爬虫使用脚本登录Github并查看信息
2022-05-02 13:09:28
![](https://img.aspxhome.com/file/2023/9/83899_0s.png)
Golang 高效排序数据详情
2023-06-23 08:23:41
![](https://img.aspxhome.com/file/2023/6/87626_0s.jpg)
python Web开发你要理解的WSGI & uwsgi详解
2021-02-04 08:46:38
![](https://img.aspxhome.com/file/2023/1/72981_0s.png)