I have two NumPy arrays:
one named labels and the other component:
labels = array([ 0, 0, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 3, 4, -1])
component = array([ 1.05325312, 1.0206622 , 1.0372349 , 1.06951778, 0.96379751, 0.98862576, 1.01135931, 0.92633951, 1.09095756, 1.1662432, 1.17794883, 1.23006966, 1.25465147, 1.27054648, 1.18940802, 0.91512676, 0.81926385])
I want the labels array to be the keys of the dictionary, and the elements in components to be sorted into the keys.
Both arrays are the same shape and the positions of the elements in labels correspond to the position in components.
I'm trying to get something like this:
{'-1': [0.81926385],
'0': [1.05325312, 1.0206622, 1.0372349, 0.96379751, 0.98862576, 1.01135931, 1.09095756],
'1': [0.92633951, 1.1662432],
'2': [1.25465147, 1.27054648],
'3': [1.06951778, 1.18940802],
'4': [0.91512676]}
I have tried using zip with several different methods but I can't figure out how to split the values into their associated key. Can anyone point me in the right direction?
d = dict(zip(labels, components))