简体   繁体   English

如何从python中的两个ndarray列表创建键值对列表?

[英]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.请注意,如果ab是 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.

相关问题 如何根据具有两个键值对的词典列表中的另一个kv对的值创建一个键值对中的值列表 - how to create a list of values from one key-value pair based on the value of another k-v pair from a list of dictionaries with two key-value pairs 如何从键值对列表中搜索数据是否在列表中 - How to Search data from a list of Key-Value pair that it is in list or not 如何将现有列表转换为 Python 中字典的键值对? - How to turn an existing list into a key-value pair for a dictionary in Python? 如何根据 Python 中的公共键值对有效地将键值从一个字典列表插入另一个字典列表? - How to efficiently insert key-value from one list of dictionaries to another based on a common key-value pair in Python? 根据来自单独列表的匹配项,使用来自字典列表中的值的键值对创建一个新字典 - Create a new dictionary with the key-value pair from values in a list of dictionaries based on matches from a separate list 如何从列表中创建一个字典,只包含键列表和键值对(Python)? - How to create a dictionary from a list with a list of keys only and key-value pairs (Python)? 列表理解 - 尝试从键值对数组的数组中创建字典 - List comprehension - attempting to create dictionary from array of key-value pair arrays 如何从键值对列表创建 Spark Row - How to create Spark Row from list of key-value pairs 如何从 python 中的键值对中搜索键 - How to search key from key-value pair in python 在不使用列表、迭代器的情况下从 Python 中的字典中检索第一个键值对 - Retrieve first key-value pair from a dictionary in Python without using list, iter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM