1

I have a list of strings which form mathematical expressions, for example:

my_list = ["23*a*(1-b)", "19*32-4", ..., "0.3232*(1-0.53)*(-1)"]

Each string is actually very long (in the order of 100 - 25000 'lines' of code), and I have a lot of strings such as this. When I save each string to a text file, and then make a function out of it by copy and paste, it generally takes too long to formulate the return statement.

def f(args):
   return (long string contents)

i.e. the return statement can be tens of thousands of terms long and in Jupyter notebooks, my cell never stops processing, even after around 20 minutes.

Apart from sitting here and unpacking all the terms into lines and then aggregating those lines, what can I do?

5
  • Can you be a bit more descriptive about the contents of the expressions? What do they contain? Is it only numbers and simple variables? which operations? Commented Jan 13, 2020 at 14:17
  • please add a reproducable example Commented Jan 13, 2020 at 14:17
  • Plus, how do you concatenate each expression with the rest? Commented Jan 13, 2020 at 14:18
  • more info on the kind of output you expect Commented Jan 13, 2020 at 14:21
  • I think it will take time as you mentioned because there are many reasons like computing power and programing techniques. I think you are using right programing techniques according to your explanation and there is a huge data which you are using then i think it is obvious to take some time. Commented Jan 13, 2020 at 14:26

3 Answers 3

2

Given your my_list, I would proceed like this if RAM memory is not an issue:

my_results = []
for expression in my_list:
    my_results.append(eval(expression))  # e.g.: eval("2+2") = 4

# my_results = [eval(e) for e in my_list] # If you prefer a one liner

If, on the contrary, you have some RAM limitations, you can write each result into a file:

f = open("results.txt", "w")
for expression in my_list:
    f .write(eval(expression))  # e.g.: eval("2+2") = 4

The advantage of writing into a file is that if the process is stuck or runs out of memory, you still get all the previously computed values.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this idea, in conjunction with eval() (which I didn't know about) will solve the issue, or provide a good starting point. Many thanks.
2

You can use eval(). Eval takes a string as an argument and converts it into an expression and computes it. In your case Program will look like

my_list = ["17*7","2+2"]
results = []
for i in range(len(my_list)):
   result.append(eval(my_list[i]))
print(result)

Comments

1

have you tried to write the function directly in a text file and then importing it?

like

file: file1.py

def f1(args):
    return (the long string solution)

inside your jupyter notebook:

from file1 import f1
f1(args)

if your is only a 'copy and paste into jupyter' problem it should work

2 Comments

Thank you for your answer, I had tried this, but the files have not imported into Jupyter either.
were you able to import the file? if not here is a guide: mg.readthedocs.io/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.