How to subtract every component available as tuples from a list by list's first value?

Question:

How to subtract a every values available in list containing tuples by a list’s first value (which is also tuple)?

For example,

I have a list which contains number of tuples. Every tuple consists X and Y coordinates.

tuple1 = [(57.4, 213.0), (57.4, 214.0), (58.4, 214.0), (58.4, 213.0), (59.4, 213.0), (60.4, 213.0), (61.4, 213.0), (61.4, 214.0), (60.4, 214.0), (59.4, 214.0), (59.4, 215.0), (60.4, 215.0), (61.4, 215.0), (61.4, 216.0), (60.4, 216.0), (60.4, 217.0), (61.4, 217.0), (59.4, 217.0), (59.4, 216.0)]

I want to subtract every value available in list by list’s first value, in this case is tuple1[0] = (57.4. 213.0).

So from 1st component of every tuple, I want to subtract 57.4 and from the second component, I want to subtract 213.

How would I do this?

Asked By: Urvesh

||

Answers:

Just do it with a list comprehension:

[(t[0]-tuple1[0][0], t[1]-tuple1[0][1]) for t in tuple1]
Answered By: Marcus

Since it worked, I put it as an answer:

finalTuple1 = tuple(numpy.subtract(tuple1, tuple1[0]))

Note that you have to import:

import numpy
Answered By: michalis
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.