0

Having the following nested array

[[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5]], [[1, 0], [1, 1], [1, 2],   [1, 3], [1, 4], [1, 5]], [[2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], [[3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5]], [[4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5]], [[5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]]

I'd like to remove subarray containers until it becomes a 2 dimensional array like:

[[0,0], [5,1], [5,4]...]

.flatten removes everything and I need to keep the groups of 2 within subarrays.

5
  • 2
    If a is your array you say that you want to produce the array [[0,0], a[5][1], a[5][4]...]. What is the rule for constructing that array? Commented Oct 21, 2016 at 15:30
  • using the matrix class Commented Oct 21, 2016 at 15:44
  • You selected an answer that generates an array beginning [[0, 0], [0, 1], [0, 2]..., but you said it should be [[0,0], [5,1], [5,4]...]. Please explain. Commented Oct 21, 2016 at 17:21
  • The answer below has been marked as correct. The code I typed was only meant to be a brief example of what I needed. Commented Oct 21, 2016 at 17:51
  • The downvote was mine. What you need is a better example, say: "given the 2-element array arr = [[ [0,0], [0,1]], [[0,2], [0,3]]] how can I produce the 4-element array [[0,0], [0,1], [0,2], [0,3]]". Note three things: 1) the example is complete (no "..."); 2) the example is no larger than necessary; and 3) I have assigned a variable (arr) to the input object, so readers can use that variable in their answers and comments without having to define it (not necessary for outputs). Commented Oct 21, 2016 at 21:23

1 Answer 1

8

also, next time you can try to read documentation :)

a = [[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5]], [[1, 0], [1, 1], [1, 2],   [1, 3], [1, 4], [1, 5]], [[2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], [[3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5]], [[4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5]], [[5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]]

a.flatten(1)
>[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]
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.