0

I have a 2D array and a 3D array. I am trying to zip them together.

This is the code.

data1 = [
    [1, 1.0, 1.5, 500, 0.0, 0.0, ([[1, 2],[3, 4]])],
    [1, 1.0, 1.5, 500, 0.0, 0.5, ([[2, 3],[4, 5]])],
    [1, 1.5, 1.5, 500, 0.0, 0.0, ([[300, 499],[577, 699]])],
    [1, 1.5, 1.5, 500, 0.0, 0.5, ([[477, 599],[644, 788]])]
]

data2 = [
    [[0, 0, 90], [2, 3, 5]],
    [[4, 7, 8], [8, 4, 6]]
]


combination = [[(k, l) for k, l in zip(data1, row)] for row in data2]

print(combination)

It results in:

[[([1, 1.0, 1.5, 500, 0.0, 0.0, [[1, 2], [3, 4]]], [0, 0, 90]), 
  ([1, 1.0, 1.5, 500, 0.0, 0.5, [[2, 3], [4, 5]]], [2, 3, 5])], 
 [([1, 1.0, 1.5, 500, 0.0, 0.0, [[1, 2], [3, 4]]], [4, 7, 8]), 
  ([1, 1.0, 1.5, 500, 0.0, 0.5, [[2, 3], [4, 5]]], [8, 4, 6])]]

But I am trying to get:

[[([1, 1.0, 1.5, 500, 0.0, 0.0, [[1, 2], [3, 4]]], [0, 0, 90]), 
  ([1, 1.0, 1.5, 500, 0.0, 0.5, [[2, 3], [4, 5]]], [2, 3, 5])], 
 [([1, 1.5, 1.5, 500, 0.0, 0.0, [[300, 499],[577, 699]]], [4, 7, 8]), 
  ([1, 1.5, 1.5, 500, 0.0, 0.5, [[477, 599],[644, 788]]], [8, 4, 6])]]

How can I edit my code to achieve this? Thank you!!!

1 Answer 1

1

Use iter + zip

it=iter(data1)
[list(zip(*l)) for l in zip(zip(it,it), data2)]

Output:

[[([1, 1.0, 1.5, 500, 0.0, 0.0, [[1, 2], [3, 4]]], [0, 0, 90]),
  ([1, 1.0, 1.5, 500, 0.0, 0.5, [[2, 3], [4, 5]]], [2, 3, 5])],
 [([1, 1.5, 1.5, 500, 0.0, 0.0, [[300, 499], [577, 699]]], [4, 7, 8]),
  ([1, 1.5, 1.5, 500, 0.0, 0.5, [[477, 599], [644, 788]]], [8, 4, 6])]]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.