Python array is getting changed -


my function takes points polyline , removes multiple points along straight line segment.

the points fed in follows:

pts=[['639.625', '-180.719'], ['629.625', '-180.719'], ['619.625', '-180.719'], ['617.312', '-180.719'], ['610.867', '-182.001'], ['605.402', '-185.652'], ['601.751', '-191.117'], ['600.469', '-197.562'], ['600.469', '-207.562'], ['600.469', '-208.273']]

  pta=[none]*2   ptb=[none]*2   ptc=[none]*2   simplepts=[]   pt in pts:     if pta[0]==none:       simplepts.append(pt)       pta[:]=pt       continue     if ptb[0]==none:       ptb[:]=pt       continue     if ptb==pta:       ptb[:]=pt       continue     ptc[:]=pt     print simplepts#<--[['639.625', '-180.719'], ['605.402', '-185.652']]     # check if a, b , c on straight line     # if are, b becomes c , next point allocated c.     # if not, becomes b , next point allocate c     if testforstraightline(pta,ptb,ptc):       ptb[:]=ptc # if straight     else:       simplepts.append(ptb)       print simplepts#<--[['639.625', '-180.719'], ['617.312', '-180.719']]       pta[:]=ptb # if it's not straight 

if section not straight, ptb appended simplepts array, (correctly) [['639.625', '-180.719'], ['617.312', '-180.719']]

however, on next pass simplepts array has changed [['639.625', '-180.719'], ['605.402', '-185.652']] baffling.

i presume points in array being held reference , changing other values updates values in array.

how make sure array values retain values assigned?

thank you.

you appending list ptb in simplepts , modifying in place.not sure if can improve design. quick solution current design-

import copy simplepts.append(copy.deepcopy(ptb)) 

Comments