What is a subtraction function that is similar to sum() for subtracting items in list?
Asked Answered
P

4

15

I am trying to create a calculator, but I am having trouble writing a function that will subtract numbers from a list.
For example:

class Calculator(object):
    def __init__(self, args):
        self.args = args

    def subtract_numbers(self, *args):
        return ***here is where I need the subtraction function to be****

For addition, I can simply use return sum(args) to calculate the total but I am unsure of what I can do for subtractions.

Purgative answered 1/4, 2012 at 10:0 Comment(3)
How would you define a "subtract" function? For sum, it's intuitive. You just add all the numbers. But for subtraction, what are you subtracting from and what are you subtracting?Orleanist
You first need to define what subtraction function is for multiple arguments. is it first_element - sum(rest_of_elements)? Is it sum(args[i] * (-1)^1? is it abs(max(list) - sum(rest_of_elements))? As you can see, there are endless number of possibilities - what exactly are you after?Clip
How exactly is it supposed to work? You want to subtract those numbers in the list from what?Unroot
C
21
from functools import reduce  # omit on Python 2
import operator

a = [1,2,3,4]

xsum = reduce(operator.__add__, a)  # or operator.add
xdif = reduce(operator.__sub__, a)  # or operator.sub

print(xsum, xdif)
## 10 -8

reduce(operator.xxx, list) basically "inserts" the operator in-between list elements.

Cottar answered 1/4, 2012 at 10:32 Comment(2)
Is there a reason why you prefer the underscores version of the operator names?Dolf
@Marcin: none, except the underscores are sexy ;))Cottar
F
7

It depends exactly what you mean. You could simply subtract the sum of the rest of the numbers from the first one, like this:

def diffr(items):
    return items[0] - sum(items[1:])

It's tough to tell because in subtraction it's dependent on the order in which you subtract; however if you subtract from left to right, as in the standard order of operations:

x0 - x1 - x2 - x3 - ... - xn = x0 - (x1 + x2 + x3 + ... + xn)

which is the same interpretation as the code snippet defining diffr() above.

It seems like maybe in the context of your calculator, x0 might be your running total, while the args parameter might represent the numbers x1 through xn. In that case you'd simply subtract sum(args) from your running total. Maybe I'm reading too much into your code... I think you get it, huh?

Frostbite answered 1/4, 2012 at 10:6 Comment(0)
F
0

Subtract function is same as sum having negative signs like this

x = 1
y = 2
sum([x, y])

>>> 3

sum([x, -y])

>>> -1

for more numbers

a = 5
b = 10
c = 15
sum([a, b, c])
sum([a, -b, -c])

In general, Subtract function is formed by changing you list signs like this

l = [1, 2, 3, 4, ...., n]
new_l = l[0] + [-x for x in l[1:]

# Or
new_l = [-x for x in l]
new_l[0] = -newl[0]

# Or one liner,
new_l = [-x for x in l if x != l[0]]
Favour answered 6/8, 2022 at 8:37 Comment(0)
D
0

With sub from operator👇

from functools import reduce
from operator import sub

class Calculator:
    """
    Usage

    ```py
    calculator = Calculator(45, 72, 28, 10)
    substraction = calculator() # -65
    ```
    """

    def __init__(self, *args):
        self.args = args

    def subtract_numbers(self) -> int:
        # return ***here is where I need the subtraction function to be****
        output = reduce(
            sub, [digit for digit in self.args if digit and isinstance(digit, int)]
        )
        print("Calculator%s --> %s" % (self.args, output))
        return output

    def __call__(self) -> int:
        return self.subtract_numbers()


calculator = Calculator(45, 72, 28, 10)
substraction = calculator()

Will output -65 equivalent to = 45-72-28-10

Dobrinsky answered 31/7, 2024 at 16:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.