0

I am trying to make key-value pair from 2 ndarray in my data. To do so, I tried to loop through each list of ndarray try to put them together, but it is not quite working for me. Can anyone suggest possible way of doing this? Any idea?

data and my attempt

here is first list of ndarray that I need to make dictionary from it:

['267']
['354' '783']
['21488']
['6063A']
['86R']
['969']
['332']
['630']
['788']
['8']
['27']
['278']
['262' '86K']

here is second list of ndarray:

['JBS']
['Cargill' 'Harris Ranch']
['One World']
['Central Valley']
['Cargill']
['JBS']
['FPL']
['CS Beef']
['Aurora Pack']
['National Beef']
['Creekstone']
['Tyson']
['National Beef' 'Cargill']

my attempt:

here is my attempt:

import numpy as np

for (i,j), value in np.ndenumerate(first_ndarray):
   for(m,n), value_2 in np.ndenumerate(first_ndarray):
       dict= to_dict(value, value_2)

but this is not working for me, looks the way of iterating ndarray might be right but making key-value pair from it is not happen. Can anyone point me out how to make this happen? Any thoughts?

desired output

here is the output that I want:

['JBS': '267']
['Cargill': '354' , 'Harris Ranch': '783']
['One World': 21488']
['Central Valley':'6063A']
['Cargill':'86R']
['JBS':'969']
['FPL':'332']
['CS Beef':'630']
['Aurora Pack':'788']
['National Beef':'8']
['Creekstone':'27']
['Tyson':'278']
['National Beef':'262', 'Cargill':'86K']

how can I get my desired output? Any idea? thanks!

Adam
  • 223
  • 1
  • 14
  • 4
    Your output is not a dictionary. It is not any default Python object for what I can see. Please correct your question. – alec_djinn Sep 14 '20 at 20:11
  • @alec_djinn right, I meant to say key-value pair from ndarray list. I updated my post with correction. Thanks! – Adam Sep 14 '20 at 20:13
  • The example is still not a Python object. `['...': '...']` isn’t a thing. Please update and clarify. – S3DEV Sep 14 '20 at 20:15
  • @S3DEV what do you mean not a things? It does happen and sure python might do this job. – Adam Sep 14 '20 at 20:18
  • Open for discussion, but I get a syntax error with `['JBS': '267']`. – S3DEV Sep 14 '20 at 20:24

2 Answers2

1

You first have to flatten the lists, then you can use zip() to make a list of tuples (k, v), then convert them into a dictionary.

a = [
    ['A'],
    ['B'],
    ['C','D']
]

b = [
    [1],
    [2],
    [3,4]
]


flatten = lambda l: [item for sublist in l for item in sublist]
            
d = dict(zip(flatten(a), flatten(b)))
print(d)

{'A': 1, 'B': 2, 'C': 3, 'D': 4}

Note that if a and b are numpy arrays, you can use the flatten method/function directly. It will probably be way faster than a lambda function.

alec_djinn
  • 10,104
  • 8
  • 46
  • 71
1
first_ndarray = [['267'],
['354', '783'],
['21488' ],
['6063A'  ],
['86R' ],
['969' ],
['332'],
['630' ],
['788' ],
['8'  ],
['27'],
['278'],
['262' ,'86K']]


second_ndarray = [['JBS'],
['Cargill' ,'Harris Ranch'],
['One World'],
['Central Valley'],
['Cargill'],
['JBS'],
['FPL'],
['CS Beef'],
['Aurora Pack'],
['National Beef'],
['Creekstone'],
['Tyson'],
['National Beef', 'Cargill']]


res={}
for i in range(len(first_ndarray)):
  values = first_ndarray[i]
  keys=second_ndarray[i]
  dictionary = dict(zip(keys, values))
  res.update(dictionary)

print(res)

Output:

{'Aurora Pack': '788',
 'CS Beef': '630',
 'Cargill': '86K',
 'Central Valley': '6063A',
 'Creekstone': '27',
 'FPL': '332',
 'Harris Ranch': '783',
 'JBS': '969',
 'National Beef': '262',
 'One World': '21488',
 'Tyson': '278'}
ashraful16
  • 2,742
  • 3
  • 11
  • 32