0

I have a big array named A of arrays which follows the following structure :

[ [0.453,0.5452,0.252], [0.411,0.352,0.119], [...], ... , [...] ]

I have an other array named B with the same length but filled with string. For example :

['toto1','toto2',...,'totoN']

Now I would like to associate each array of A with a string of B. So either like this :

[ ['toto1',0.453,0.5452,0.252], ['toto2',0.411,0.352,0.119], [...], ... , [...] ]

Or :

{'toto1':[0.453,0.5452,0.252],'toto2':[0.411,0.352,0.119],...}

depending of what it is possible to do and the most easiest way to do it.

First, is it possible to do this kind of thing ? If yes, could you please help me to figure out how I can achieve that ?

Thank you very much in advance

lilouch
  • 1,054
  • 4
  • 23
  • 43
  • This is clearly not a duplicate of the linked question since the OP is asking to combine strings and numbers in a numpy array. Looking at the questions title it must be obvious that the dictionary serves only as a fallback here. – Paul Panzer Feb 20 '17 at 15:30
  • Are you talking about numpy arrays, or Python lists (of lists)? The distinction is important - unless you are happy with zipped dictionary construction. – hpaulj Feb 20 '17 at 15:43
  • @hpaulj it says "numpy array" in the title – Paul Panzer Feb 20 '17 at 15:45
  • numpy arrays can't have mixed types, unless the dtype='O', which is probably not what you want. but for what you are specifically trying to do, take a look at pandas DataFrames. – Corley Brigman Feb 20 '17 at 15:45
  • We could go the structured array route. That's why we need clarification. Is it important that A be 2d? Or is `array of arrays` just javascript talk for a nested list? I can reopen this if it matters. – hpaulj Feb 20 '17 at 15:46
  • @CorleyBrigman Can't they? Try this `a = np.zeros((3,), dtype=[('f0', ' – Paul Panzer Feb 20 '17 at 15:52
  • interesting, I didn't know that (most of my 'numpy' knowledge is really pandas, where they don't even try this). but it is a different kind of array: `np.append(a, 0)` gives an exception, and trying to do e.g `a[0] + a[1]` gives an exception as well (but `a2 = np.zeros(4, dtype=' – Corley Brigman Feb 20 '17 at 16:00
  • @CorleyBrigman Yeah I'm unsure myself what they are really good for, except for setting up a make shift `unique_rows` (sorting is one of the few operations those compound dtypes support) which actually is quite useful. – Paul Panzer Feb 20 '17 at 16:08
  • Pandas is another way of adding labels to an array – hpaulj Feb 20 '17 at 16:10

3 Answers3

2

If you are looking for a dictionary, you can use zip:

dict(zip(B, A))
# {'toto1': [0.453, 0.5452, 0.252], 'toto2': [0.411, 0.352, 0.119]}
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Thank you for your answer. And if B is a list ? Will it be the same ? – lilouch Feb 20 '17 at 15:47
  • 1
    This should work for both lists and numpy arrays. But you will end up with different data types for the dictionary values depending on if they are lists or numpy arrays. – Psidom Feb 20 '17 at 15:52
1
associated_dict = {k:v for k,v in [(B[i], A[i]) for i in range(len(A))]}

Note that the two lists must have the same number of elements.

francisco sollima
  • 7,952
  • 4
  • 22
  • 38
1

You can run through with a loop or just use the zip:

a = [ [0.453,0.5452,0.252], [0.411,0.352,0.119]] 

b =  ['toto1','toto2']

res = {}
for i in range(len(a)):
  res[b[i]] = a[i]

print (res)

Or in the shorter way:

dict(zip(b,a))
omri_saadon
  • 10,193
  • 7
  • 33
  • 58