Comparing every line in a list with the previous one (multiple numbers per line) - python

2653 views python
6

I have a list like so :

(478.9013929574081, 397.2738906303129, 448.89480301797414, 542.7951217324505)
(472.90651700176176, 424.78297261394266, 502.43464632003395, 603.6891367440179)
(468.10938689187105, 427.371498928372, 511.24005916622326, 614.6921165073483)
(461.6597495547769, 421.4376873975313, 507.23734519352905, 611.8902878449529)
(425.487279262123, 402.292710366325, 507.77891332142167, 621.1597417845782)
(419.40784031782096, 398.2749509071074, 506.50067770937756, 621.1464694579362)
(413.0082657866075, 397.6126602713513, 510.7013095813061, 627.1330222592819)
(413.0683323863853, 398.88024170228, 512.6260305724528, 629.2101773020437)
(422.5678390801497, 406.35400653231824, 516.6077609026671, 631.4456260240206)
(415.5577803083175, 401.4576799299969, 514.6341115538066, 630.8472626355573)
(418.55910283476703, 404.5636199237953, 517.0606565634685, 632.8283515818844)
(434.41278792007824, 420.3742027152652, 529.0694380783063, 642.42857214518)
(415.3420769027216, 404.21410273005847, 519.1040751582156, 635.7586341103566)
(443.0505653390292, 432.6358785944982, 541.5291344399056, 654.1970677458233)
(465.30516533741354, 431.5888053340561, 520.7964063717009, 625.9943265634903)
(419.0982438430095, 388.7972967913932, 491.81636612895943, 605.2795535885007)
(427.7796057482705, 392.0655443853052, 489.5256797085785, 600.7623416078488)
(454.50745633403204, 411.26272364904656, 496.8873392060136, 602.2765376994458)
(445.03208123588115, 402.5587576107865, 491.1756847902183, 598.375762651814)
(470.62598009859505, 431.7971898284693, 516.3417600231103, 619.7167200776199)
(467.00822669571744, 428.97165850610526, 514.9142489788748, 619.044977203982)
(429.377607418294, 400.45615209690385, 501.56268775922575, 613.4860469091849)
(434.0340968876508, 392.2060647938512, 484.2164776843913, 593.4859705680318)
(428.7396884234395, 385.71715081057516, 478.4743675782727, 588.5386312124532)
(437.47633322723084, 395.40554135462384, 486.3183547162734, 594.9332249370034)
(460.586968864012, 417.3971201234364, 501.49811154912476, 605.822049687314)

As you can see each line is essentially 4 elements. What I want to do is compare each element in each line with the previous line's elements. The goal here is to notice a decrease/increase in any one of the 4 elements as each element represents distance, and an increase/decrease will demonstrate movement. I only want to compare each line with the previous one.

Here is what I attempted (without any luck):

 for i in distance_from_goal:
   try:
     if(distance_from_goal[i] < distance_from_bins[i+1]):
       print('reduced!')
     else:
       print('not reduced')
   except:
     pass

I feel like I am missing something fundamental here as I attempt to compare the entire row. Kindly advice on how to proceed with this

answered question

1 Answer

2

You need the index of lines and index of each item in a line:

for i in range(len(distance_from_goal) - 1):
  for j in range(4):
    if(distance_from_goal[i][j] > distance_from_bins[i+1][j]):
      print(j, 'is reduced!')
    else:
      print(j, 'is not reduced')

posted this

Have an answer?

JD

Please login first before posting an answer.