如何从python中的字符串中拆分整数和运算符字符? [英] How to split the integers and Operators characters from string in python?

查看:105
本文介绍了如何从python中的字符串中拆分整数和运算符字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串拆分为整数和运算符,以便在 python 中进行中缀表达式计算.

这是我的字符串:

<预><代码>>>>s = (1-2+3)*5+10/2

我试过这样拆分:

>>>list(s)['(', '1', '-', '2', '+', '3', ')', '*', '5', '+', '1', '0', '/', '2']

这是错误的.由于'10'被拆分为'1','0'

我尝试了替代方法:

<预><代码>>>>re.findall('[+-/*//()]+|\d+',s)['(', '1', '-', '2', '+', '3', ')*', '5', '+', '10', '/', '2']

这也出错了.由于 ')*' 应该拆分为 ')', '*'

你能帮忙从给定的表达式中拆分运算符和整数吗?

解决方案

这不是中缀的最佳解决方案.删除 [] 后的 +,如:

导入重新s = "(1-2+3)*5+10/2"print re.findall('[+-/*//()]|\d+',s)['(', '1', '-', '2', '+', '3', ')', '*', '5', '+', '10', '/', '2']

尝试以下链接以获得正确的解决方案:简单的平衡括号

from pythonds.basic.stack import Stackdef postfixEval(postfixExpr):操作数堆栈 = 堆栈()tokenList = postfixExpr.split()对于 tokenList 中的令牌:如果令牌在0123456789"中:操作数堆栈.push(int(token))别的:操作数 2 = 操作数堆栈.pop()操作数 1 = 操作数堆栈.pop()结果 = doMath(token,operand1,operand2)操作数堆栈.推(结果)返回操作数Stack.pop()def doMath(op, op1, op2):如果操作 == "*":返回 op1 * op2elif op == "/":返回 op1/op2elif op == "+":返回 op1 + op2别的:返回 op1 - op2打印(postfixEval('7 8 + 3 2 +/'))

请记住,这是一个后缀实现,仅作为示例.自己做中缀,如果你有任何困难,尽管问.

I want to split the string into integers and operators for doing Infix expression evaluation in python.

Here is my string:

>>> s = (1-2+3)*5+10/2

I tried this to split:

>>>list(s)
['(', '1', '-', '2', '+', '3', ')', '*', '5', '+', '1', '0', '/', '2']

This is wrong. Since '10' is splitted into '1','0'

I tried alternative:

>>> re.findall('[+-/*//()]+|\d+',s)
['(', '1', '-', '2', '+', '3', ')*', '5', '+', '10', '/', '2']

This is also went wrong. Since ')*' should be splitted into ')', '*'

Could you help to split the operators and integers from the given expression?

解决方案

This is not the best solution for infix. Remove the + after [] like:

import re
s = "(1-2+3)*5+10/2"
print re.findall('[+-/*//()]|\d+',s)

['(', '1', '-', '2', '+', '3', ')', '*', '5', '+', '10', '/', '2']

Try the following link for correct solution: Simple Balanced Parentheses

from pythonds.basic.stack import Stack

def postfixEval(postfixExpr):
    operandStack = Stack()
    tokenList = postfixExpr.split()

    for token in tokenList:
        if token in "0123456789":
            operandStack.push(int(token))
        else:
            operand2 = operandStack.pop()
            operand1 = operandStack.pop()
            result = doMath(token,operand1,operand2)
            operandStack.push(result)
    return operandStack.pop()

def doMath(op, op1, op2):
    if op == "*":
        return op1 * op2
    elif op == "/":
        return op1 / op2
    elif op == "+":
        return op1 + op2
    else:
        return op1 - op2

print(postfixEval('7 8 + 3 2 + /'))

Keep in mind that this is a postfix implementation and its just for example. Do the infix by yourself and if you have any difficulties just ask.

这篇关于如何从python中的字符串中拆分整数和运算符字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆