[英]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.我正在尝试从我的数据中的 2 ndarray 制作键值对。 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.为此,我尝试遍历 ndarray 的每个列表,尝试将它们放在一起,但这对我来说不太奏效。 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:这是我需要从中制作字典的第一个 ndarray 列表:
['267']
['354' '783']
['21488']
['6063A']
['86R']
['969']
['332']
['630']
['788']
['8']
['27']
['278']
['262' '86K']
here is second list of ndarray:这是 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.但这对我不起作用,看起来迭代 ndarray 的方式可能是正确的,但不会发生键值对。 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.您首先必须展平列表,然后可以使用zip()
制作元组 (k, v) 列表,然后将它们转换为字典。
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.请注意,如果a
和b
是 numpy 数组,则可以直接使用flatten 方法/函数。 It will probably be way faster than a lambda function.它可能比 lambda 函数快得多。
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'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.