Turning things in strings into operations?

This is the place for queries that don't fit in any of the other categories.

Turning things in strings into operations?

Postby cakefiend » Wed Apr 23, 2014 9:26 am

Is there any way to turn an operation that is in a string into an operation? E.g. if somebody entered as an input 23*59 it would be a string. I have a function that separates things into a list so 23*59 would turn into ["23", "*", "59"]. At the moment I would be using lots of ifs and elifs to say for example
Code: Select all
if calc[n] == "*":
    return float(calc[n-1])*float(calc[n+1])

etc.
Is there an easier way of doing this? Like how int(calc[n]) would turn the stringy number into a float, is there anything like that for operations?
Thanks
Last edited by stranac on Wed Apr 23, 2014 9:40 am, edited 1 time in total.
Reason: Added code tags. Locked.
cakefiend
 
Posts: 5
Joined: Wed Apr 23, 2014 9:22 am

Re: Turning things in strings into operations?

Postby stranac » Wed Apr 23, 2014 9:43 am

Please read this to learn how to post code: viewtopic.php?t=145

You can use a dict with functions in it.
You can also take a look at the operator module, where you can find function implementations of python operators.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Turning things in strings into operations?

Postby cakefiend » Wed Apr 23, 2014 10:21 am

Thank you, sorry for posting the code badly.
cakefiend
 
Posts: 5
Joined: Wed Apr 23, 2014 9:22 am

Re: Turning things in strings into operations?

Postby cakefiend » Wed Apr 23, 2014 10:33 am

I'm not sure how to put a function into a dictionary but it doesn't seem to be working for me :(
cakefiend
 
Posts: 5
Joined: Wed Apr 23, 2014 9:22 am

Re: Turning things in strings into operations?

Postby cakefiend » Wed Apr 23, 2014 10:38 am

This is my function so far

Code: Select all
def calculate(calc):
    while len(calc) > 1:
        while "**" in calc:
            n = calc.index("**")
            calc[n] = float(calc[n-1])**float(calc[n+1])
            calc.pop(n+1)
            calc.pop(n-1)
        while "//" in calc:
            n = calc.index("//")
            calc[n] = float(calc[n-1])//float(calc[n+1])
            calc.pop(n+1)
            calc.pop(n-1)
        while "/" in calc:
            n = calc.index("/")
            calc[n] = float(calc[n-1])/float(calc[n+1])
            calc.pop(n+1)
            calc.pop(n-1)
        while "*" in calc:
            n = calc.index("*")
            calc[n] = float(calc[n-1])*float(calc[n+1])
            calc.pop(n+1)
            calc.pop(n-1)
        while "+" in calc:
            n = calc.index("+")
            calc[n] = float(calc[n-1])+float(calc[n+1])
            calc.pop(n+1)
            calc.pop(n-1)
        while "-" in calc:
            n = calc.index("-")
            calc[n] = float(calc[n-1])-float(calc[n+1])
            calc.pop(n+1)
            calc.pop(n-1)
    for n in calc:
        return n


calc is a list
I've done it this way so that the order of operations works.
I just think that it would be more efficient using a dictionary but I don't know how to do it.
cakefiend
 
Posts: 5
Joined: Wed Apr 23, 2014 9:22 am

Re: Turning things in strings into operations?

Postby stranac » Wed Apr 23, 2014 10:52 am

Something like this:
Code: Select all
>>> import operator
>>> op = {'+': operator.add,
...       '-': operator.sub,
...       '*': operator.mul,
...       '/': operator.div,
...       '**': operator.pow,
...       }
>>> func = op['*']
>>> func(2, 3)
6

Of course, you would modify that to suit your needs.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Turning things in strings into operations?

Postby cakefiend » Wed Apr 23, 2014 1:15 pm

Thank you! :-)
cakefiend
 
Posts: 5
Joined: Wed Apr 23, 2014 9:22 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests