4

I have a dict of numpy arrays :

{'data1': array([[0.16461831, 0.82400555],
        [0.02958593, 0.483629  ],
        [0.51268564, 0.07030046],
        [0.17027816, 0.35304705]]),
 'data2': array([[0.8292598 , 0.78136548],
        [0.30389913, 0.69250432],
        [0.66608852, 0.42237639],
        [0.72678807, 0.40486951]]),
 'data3': array([[0.45614633, 0.96677904],
        [0.87066105, 0.75826116],
        [0.39431988, 0.73041888],
        [0.65685809, 0.65498308]])}

Expected output :

[([0.16461831, 0.82400555], [0.8292598 , 0.78136548], [0.45614633, 0.96677904]), 
 ([0.02958593, 0.483629 ],  [0.66608852, 0.42237639], [0.87066105, 0.75826116]), 
 ([0.51268564, 0.07030046], [0.66608852, 0.42237639], [0.39431988, 0.73041888]), 
 ([0.17027816, 0.35304705], [0.72678807, 0.40486951], [0.65685809, 0.65498308])]

But when I am trying with zip :

list(zip(data.values()))

Getting this output:

[(array([[0.16461831, 0.82400555],
         [0.02958593, 0.483629  ],
         [0.51268564, 0.07030046],
         [0.17027816, 0.35304705]]),),
 (array([[0.8292598 , 0.78136548],
         [0.30389913, 0.69250432],
         [0.66608852, 0.42237639],
         [0.72678807, 0.40486951]]),),
 (array([[0.45614633, 0.96677904],
         [0.87066105, 0.75826116],
         [0.39431988, 0.73041888],
         [0.65685809, 0.65498308]]),)]

How to zip list of numpy arrays?

1
  • 1
    @ggorlen anything is fine, a tuple of numpy array or tuple of lists. Commented Jul 10, 2020 at 16:35

4 Answers 4

6

If a 3D array works for you, you can just stack on the 2nd axis (axis=1):

np.stack(data.values(), axis=1)

#[[[0.16461831 0.82400555]
#  [0.8292598  0.78136548]
#  [0.45614633 0.96677904]]

# [[0.02958593 0.483629  ]
#  [0.30389913 0.69250432]
#  [0.87066105 0.75826116]]

# [[0.51268564 0.07030046]
#  [0.66608852 0.42237639]
#  [0.39431988 0.73041888]]

# [[0.17027816 0.35304705]
#  [0.72678807 0.40486951]
#  [0.65685809 0.65498308]]]
 
Sign up to request clarification or add additional context in comments.

Comments

5

Use

list(zip(*data.values())

Output:

[(array([0.16461831, 0.82400555]),
  array([0.8292598 , 0.78136548]),
  array([0.45614633, 0.96677904])),
 (array([0.02958593, 0.483629  ]),
  array([0.30389913, 0.69250432]),
  array([0.87066105, 0.75826116])),
 (array([0.51268564, 0.07030046]),
  array([0.66608852, 0.42237639]),
  array([0.39431988, 0.73041888])),
 (array([0.17027816, 0.35304705]),
  array([0.72678807, 0.40486951]),
  array([0.65685809, 0.65498308]))]

Comments

1

following code will create your output:

tmp = [data[d].tolist() for d in data]      
tmp = list(zip(*tmp))

output:

[([0.16461831, 0.82400555], [0.8292598, 0.78136548], [0.45614633, 0.96677904]), ([0.02958593, 0.483629], [0.30389913, 0.69250432], [0.87066105, 0.75826116]), ([0.51268564, 0.07030046], [0.66608852, 0.42237639], [0.39431988, 0.73041888]), ([0.17027816, 0.35304705], [0.72678807, 0.40486951], [0.65685809, 0.65498308])]

this link will explain about * syntax

Comments

1

Another way:

[tuple(x) for x in np.stack(data.values(),axis=1).tolist()]

[([0.16461831, 0.82400555], [0.8292598, 0.78136548], [0.45614633, 0.96677904]), 
 ([0.02958593, 0.483629], [0.30389913, 0.69250432], [0.87066105, 0.75826116]), 
 ([0.51268564, 0.07030046], [0.66608852, 0.42237639], [0.39431988, 0.73041888]), 
 ([0.17027816, 0.35304705], [0.72678807, 0.40486951], [0.65685809, 0.65498308])]

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.