0

I'm writing a program where I need to have numbers in a certain order with operations between them in a certain order. To illustrate, my input is 2 lists (of numbers and of operators):

number_list = [a, b, c, d]
operations_list = [+, -, *]

From these input lists, I need to slide the operators in between the numbers, keeping the same order. Like this:

a + b - c * d

*Yes, I know I can't put operators in lists like that. I'm just trying to help you guys visualize what I'm trying to do...

Thanks for any help!

Andy M
  • 97
  • 1
  • 9
  • You should be able to use `int`'s magic methods. – cs95 Aug 04 '17 at 02:34
  • More specifically, `int` has methods that can be used like so: `int.__add__(9,2) == 11` – Daniel Corin Aug 04 '17 at 02:36
  • 1
    @AshwiniChaudhary I think you misunderstood. OP wants to perform those arbitrary operations. The point is not to interleave anything here. – cs95 Aug 04 '17 at 02:37
  • @danielcorin, I don't quite see how that could help me in this scenario... – Andy M Aug 04 '17 at 02:40
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ I was adding the other dupe meanwhile, combining both will help them in solving it. First one will help in coming up with `a + b - c * d` and second one in evaluating it. There are more duplicates for solving mathematical expression in a string. – Ashwini Chaudhary Aug 04 '17 at 02:42
  • 1
    @AshwiniChaudhary I don't think this is a dupe of that either, at least, I'm not convinced you need to use something as complicated as pyparsing for this. That pertains to mathematical expressions in a string. This is about a list of arbitrary numbers, and a list of arbitrary operators, and how to get the final answer. – cs95 Aug 04 '17 at 02:44
  • @cᴏʟᴅsᴘᴇᴇᴅ It is going to be slightly complicated if you want the correct mathematical answer, simply merging them won't handle operator precedence. Your deleted answer for example gives the wrong answer mathematically. – Ashwini Chaudhary Aug 04 '17 at 02:55
  • @AshwiniChaudhary Yeah... fair enough. – cs95 Aug 04 '17 at 03:02

0 Answers0