1

Lets say have an array of the form

r=([[[3,2,1],[5,4,1]],[[10,6,3],[5,3,1]],[[9,5,2],[8,4,1]]])

And I want to do a subtraction between the elements of each array but getting the following array

r=([[[3-3,3-2,3-1],[5-5,5-4,5-1]],[[10-10,10-6,10-3],[5-5,5-3,5-1]],[[9-9,9-5,9-2],[8-8,8-4,8-1]]])

r=([[[0,1,2],[0,1,4]],[[0,4,7],[0,2,4]],[[0,4,7],[0,4,7]]])

I have tried loops inside loops but I don't get what I want because I don't know how to restart the value I'm subtracting in each array.

1
  • 1
    Is there a reason you're not using numpy? Also, you haven't show your code to get that output for us to assist with. Commented May 14, 2018 at 19:57

1 Answer 1

1

You can use a nested list comprehension as following:

In [45]: [[[i[0]-j for j in i] for i in sub] for sub in r]
Out[45]: [[[0, 1, 2], [0, 1, 4]], [[0, 4, 7], [0, 2, 4]], [[0, 4, 7], [0, 4, 7]]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.