I am trying to create a list (say B
) which increments only when there is a difference in values of another list (say A
), for example:
[1,1,2,2,4,4]
to [0,0,1,1,2,2]
or
[1,1,1,1,4,4,4,4]
to [0,0,0,0,1,1,1,1]
etc.
The following code does it:
boxes=[1,1,1,1,4,4,4,4]
positions=[0]
position=0
for psn,box in list(enumerate(boxes))[:-1]:
if boxes[psn+1]-box ==0:
increment=0
else:
increment=1
position=position+increment
positions.append(position)
print(positions)
Can anybody give suggestions to do it using list comprehensions (preferable using lambda
functions)?