recursively change values in list of lists
Medardo Rodriguez (Merchise Group)
med.swl at gmail.com
Sun Aug 24 10:47:31 EDT 2008
On Sun, Aug 24, 2008 at 10:17 AM, Carson Farmer <carson.farmer at gmail.com> wrote:
> So in the end, the only thing that should be changed is the values,
> not the lists themselves... clear as mud?
>
> Any hints, and/or suggestions are greatly appreciated,
You gave the solution yourself.
I don't know if your inner polygons can contain lines and polygons, or
only lines, so:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2008 Medardo Rodriguez (Merchise Group)
#
# This is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License (GPL) as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# :)
def MoveDelta(target, dx, dy):
for item in target:
if isinstance(item[0], list): # is item a polygon?
MoveDelta(item, dx, dy) # Recursive version
else:
assert len(item) == 2 # make sure that it is a point
item[0] += dx
item[1] += dy
if __name__ == '__main__':
sample = [[[1, 2], [3, 4]], [[[7, 8], [10, 20]], [5, 6]]]
print 'Original:', sample
MoveDelta(sample, 10, 100)
print 'Result:', sample
# Regards
More information about the Python-list
mailing list