I have a list
l = [object0, object1, object2, object3, object4....object499]
maximum length is never more than 500
All objects have attribute x, y, z, a
I have to modify object.y if duplicates in object.x as:
if object9.x == object10.x:
object9.y = object9.z * object9.a/1000
object10.y = object10.y - object9.z * object9.a/1000
It is guaranteed that duplicates will be in consecutive objects.
There may be more than 2 duplicates ie for example
object12.x == object13.x == object14.x
so modification will proceed in the same manner of the third duplicate based on the value of modified 2nd duplicate.
I have written a loop to do it, but was thinking if there is any pythonic/faster way of doing it. I am using python3.7
EDIT:
tag = None
for i, o in enumerate(l):
if tag is None:
x_a = o.x
elif x_a == o.x #duplicate found
temp = o.y
c_over = 0
c_value = o[i-1].z * o[i-1].a/1000
if c_value < o[i-1].y:
o[i-1].y = c_value
c_over = temp - c_value #carry over value
o[i] = c_over # either zero of carry over value
x_a = o.x
itertools.groupby, withoperator.attrgetter('x')as thekeyfunction.for (prev, current) in zip(l, l[1:]):