I have some multiple dimension array like this:
a= [[1,2],[2,4],[31,2]]
b= [[[1,2],[2,4],[31,2]],[[22,34],[322,323],[3454,544]]]
c= [[[[1,2],[2,4],[31,2]],[[22,34],[322,323],[3454,544]]],[[[1,2],[2,4],[31,2]],[[22,34],[322,323],[3454,544]]]]
Now I want to change the value of each [x,y] pair to [x,y-x], desired result:
a= [[1,0],[2,2],[31,-29]] ==> [1,0] = [1,(1-0)]
I tried to use generator like this(Inspired from this answer):
def flatten(ary):
for el in ary:
if isinstance(el, int):
yield ary
else:
for sub in flatten(el):
yield sub
But it does not work as expected.
How to fix it?
Note:
The operation which transform [x,y] to [x,y-x]
may be changed accordingly, for example
[x,y] ==> [x,x*y]
maybe another operation.
So I do not want to hard code the operation to the iteration.
I want something like this:
for x,y in flatten(ary):
return x,y-x
Then if necessary, I just change it to :
for x,y in flatten(ary):
return x,y+x # any operation I want