1

I was wondering if it's possible to do mathematical operation between lists of numerical variables? For example, I have..

pointA = [ 22, 44, 83 ]
pointB = [ -17, 11, -25 ]

pointC = pointA - pointB
#result: [ 5, 55, 61 ]

Or should I just create my own function? Thank you!

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
Warren Daza
  • 41
  • 1
  • 5

5 Answers5

5

Install numpy.

>>> import numpy
>>> numpy.add([ 22, 44, 83 ], [ -17, 11, -25 ])
array([ 5, 55, 58])

array objects are mostly list-compatible, but are much more powerful.

>>> pointA = numpy.array([ 22, 44, 83 ])
>>> pointB = numpy.array([ -17, 11, -25 ])
>>> pointA + pointB
array([ 5, 55, 58])
>>> pointA * pointB
array([ -374,   484, -2075])
>>> pointA.dot(pointB)
-1965

Supports tons of other operations, matrices and multi-dimentional arrays...

Beni Cherniavsky-Paskin
  • 9,483
  • 2
  • 50
  • 58
4

You're adding, not subtracting, to get that result ... anyway, list comprehensions and zip() will give you what you want:

>>> pointA = [22, 44, 83]
>>> pointB = [-17, 11, -25]
>>> pointC = [a + b for a, b in zip(pointA, pointB)]
>>> pointC
[5, 55, 58]
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • @squiguy in this particular case where we know we only have two integers to add, `sum()` is just adding an unnecessary function call (and is marginally less readable IMO). – Zero Piraeus May 09 '13 at 04:26
  • Sure, but then if you have more than two lists you can use the `*` in zip and expand it for any number. It's really no big difference. +1 – squiguy May 09 '13 at 04:28
4

This can be done with map:

pointC = map(lambda p1, p2: p1 + p2, pointA, pointB)

or, more simply:

from operators import add
pointC = map(add, pointA, pointB)
Tim Heap
  • 1,671
  • 12
  • 11
  • 1
    This is nice and clear and highlights that [**`map()`**](http://docs.python.org/2/library/functions.html#map) doesn't need `zip()`, which we see all the time! (If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.) – johnsyweb May 09 '13 at 04:13
  • If more than two points must be added, `zip()` the points and `map()` over them using `sum()` (i.e. `map(sum, zip(pointA, pointB, pointC, ...))`), or make a new vararg adder function: `add_all = lambda *a: sum(a)` – Tim Heap May 09 '13 at 04:22
1

Something like this perhaps:

In [1]: def calculate(p1, p2):
   ...:     return map(sum, zip(p1, p2))

In [2]: pointA = [ 22, 44, 83 ]
   ...: pointB = [ -17, 11, -25 ]

In [3]: calculate(pointA, pointB)
Out[3]: [5, 55, 58]
root
  • 76,608
  • 25
  • 108
  • 120
  • The `operator` module contains a function for every python operator. `map()` has a multi-argument form so you don't need `zip()`: `map(operator.add, [ 22, 44, 83 ], [ -17, 11, -25 ]) == [5, 55, 58]` – Beni Cherniavsky-Paskin May 09 '13 at 04:26
  • @BeniCherniavsky-Paskin - Of course it has. However, I think in this case this is still a viable alternative. – root May 09 '13 at 04:34
0

I've been working on a linear algebra module in Python that could be used for this. It is lightweight and easy to use. The add() method allows you to add a list of matrices, in the event that you want to add more than two points.

Check it out here: https://github.com/jeremynealbrown/mbyn

A = [
            [8, 3, 4],
            [21, 3, 7],
            [3, 5, 2]
    ]

B = [
            [5, 3, 1], 
            [1, 9, 4],
            [3, 6, 1]
    ]

mbyn.add([A, B])

#output
#13, 6, 5
#22, 12, 11
#6, 11, 3
JeremyFromEarth
  • 14,344
  • 4
  • 33
  • 47
  • 1
    No offense, but what advantage does this offer the OP over a robust, tested and debugged library like `numpy`? If there is one, you should update your question to reflect this. – Hooked May 09 '13 at 04:47
  • None taken. I would not suggest that someone replace numpy with the module I'm working on. Anyone who visits my github repo will see that the readme clearly states that it is a project that I'm working on as a means to learning linear algebra. That said, numpy could be considered to be a bit overkill for the simple problem this question seeks to solve. Because of this, I see utility in the simple add method used in the example above. – JeremyFromEarth May 09 '13 at 05:07
  • Protip #5468 - Don't reinvent the wheel. – Burhan Khalid May 09 '13 at 09:29
  • @BurhanKhalid Thanks for the "Protip". As mentioned above and as noted in the linked repo: My aim is to learn linear algebra and I'm doing that through implementing simple algebraic algorithms in Python. I'm not setting out to re-invent the wheel. Did you even read my answer or following comment? – JeremyFromEarth May 09 '13 at 15:06
  • @jeremynealbrown, the github link is broken, could you update it? – astay13 Aug 10 '16 at 20:58