简体   繁体   中英

How to create key-value pair list from two list of ndarray in python?

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!

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.

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'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM