Python: how to revise tuples in an list? -
i have list of x,y tuples. think of these original tuples being change in x , y. wish apply these subsequent changes in x , y previous tuple. ex, after 0 term, x gained 100 , y stayed same. new first term (100,100). after that, x stayed same , y decreased 100. applying revised first term result in revised second term of (100,0) , etc.
xy = [(0,100), (100,0), (0,-100), (-100,0)]
this desired result:
xy = [(0,100), (100,100), (100,0), (0,0)]
how work around immutabilty of lists? how done in loop?
the list still mutable; replace tuples new ones:
x, y = 0, 0 i, (dx, dy) in enumerate(xy): x += dx y += dy xy[i] = (x, y)
demo:
>>> xy = [(0, 100), (100, 0), (0, -100), (-100, 0)] >>> x, y = 0, 0 >>> i, (dx, dy) in enumerate(xy): ... x += dx ... y += dy ... xy[i] = (x, y) ... >>> xy [(0, 100), (100, 100), (100, 0), (0, 0)]
Comments
Post a Comment