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

Time:09-12

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?

CodePudding user response:

Just do it with a list comprehension:

[(t[0]-tuple1[0][0], t[1]-tuple1[0][1]) for t in tuple1]

CodePudding user response:

Since it worked, I put it as an answer:

finalTuple(numpy.subtract(tuple1, tuple1[0])))

Note that you have to import:

import numpy
  • Related