如何解决如何从python的`operator`模块获取数学运算符字符串
|| 以operator.add
为例:
>>>import operator as op
>>>op.add(1,2) #means 1 + 2
3
>>>op.add.__name__
\'add\'
我想要某种:
>>>op.add.math_str
\"+\"
我可以获取所有那些数学字符串\"+\",\"-\",\">\"...
模块operator
支持的运行时吗?
编辑:
>>> [eval(x) for x in [\".\".join((\"op\",x,\"__doc__\")) for x in dir(op)]]
[\'abs(a) -- Same as abs(a).\',\'add(a,b) -- Same as a + b.\',\'and_(a,b) -- Same as a & b.\',\'concat(a,b) -- Same as a + b,for a and b sequences.\',\'contains(a,b) -- Same as b in a (note reversed operands).\',\'delitem(a,b) -- Same as del a[b].\',\'delslice(a,b,c) -- Same as del a[b:c].\',\'div(a,b) -- Same as a / b when __future__.division is not in effect.\',\'str(object) -> string\\n\\nReturn a nice string representation of the object.\\nIf the argument is a string,the return value is the same object.\',
上面的代码可以列出大多数运算符字符串,这意味着我可以使用re模块列出字符串?
谢谢!
解决方法
否。建立自己的字典。
, 您可以仅使用一次教程中的下表来构建自己的映射字典一次,然后在需要时使用它即可。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。