简体   繁体   English

如何使用两个相同的键和不同的值从 Python 中的三个不同列表创建字典?

[英]How to create dictionary from three different list in Python using two same Keys and different values?

I have three lists:我有三个列表:

List containing keys is:包含键的列表是:

keys = ['testname', 'output']

List containing Values are:包含值的列表是:

value1 = ['pizza', 'dog', 'lion']
value2 = ['12.3', '356', '45.6']

My desired output is:我想要的 output 是:

{
 "Labresult":[
    { 'testname': 'pizza',
       'output': '12.3',
    },
    
    { 'testname': 'dog',
      'output': '356,'
    },
    { 'testname': 'lion',
       'output': '45.6',
    }]
 }

What I tried:我尝试了什么:

dict(zip(key, zip(value1,value2)))

Good start, but you still need a loop over the values:好的开始,但您仍然需要对这些值进行循环:

{"Labresult": [dict(zip(keys, pair)) 
               for pair in zip(value1, value2)]}

Addendum :附录

In principle you can also switch to a Pandas based approach, eg if value1 and value2 are columns of a DataFrame.原则上,您也可以切换到基于 Pandas 的方法,例如,如果value1value2是 DataFrame 的列。 However, for larger amounts of data the limiting factor for your problem will always be the fact that you need to generate the nested dicts.但是,对于大量数据,问题的限制因素始终是您需要生成嵌套字典这一事实。 Comparing the initially suggested approach with a potential pandas approach for larger amounts of data (no for-loop), it turns out that the former is much faster:将最初建议的方法与潜在的 pandas 方法进行比较,以处理大量数据(无 for 循环),事实证明前者要快得多:

import numpy as np
import pandas as pd
keys = ['testname', 'output']

value1 = list(np.random.choice(['pizza', 'dog', 'lion'], 100_000))
value2 = list(map(str, np.round(np.random.random(100_000) * 1000, 1)))

%timeit {"Labresult": [dict(zip(keys, pair)) for pair in zip(value1, value2)]}
# 56 ms ± 1.8 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

df = pd.DataFrame({"testname": value1, "output": value2})
%timeit {"Labresult": df.apply(dict, axis=1).tolist()}
# 1.1 s ± 50.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

If you have the option to change the shape/data type of your desired outcome (eg no nested dicts), that could allow for improvements.如果您可以选择更改所需结果的形状/数据类型(例如,没有嵌套字典),则可以进行改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 for 循环从具有相同键但不同值的旧字典创建新字典 - How to create a new dictionary from an old dictionary with the same keys but different values using a for loop 如何使用(相同长度和相同键)两个不同字典的值创建一个新字典,用作键值对 - How to make a new dictionary from the values of (same lengths and same keys) two different dictionaries, using as key-value pair 具有多个具有不同值的相同键的字典python - Dictionary with multiple same keys with different values python 如何用Python中的键列表和值字典创建字典? - How to create a dictionary from a list of keys and a dictionary of values in Python? 替换键值与对应于同一字典中的不同键的值 - python - Replace values of keys with values corresponding to different keys in the same dictionary 如何通过迭代使用不同的键和值从另外两个字典创建一个新字典? - How to create a new dictionary from two other dictionaries taking different keys and values with iteration? Python - 如何从另一个列表创建一个字典列表,该列表具有两个具有多个值的键? - Python - How to create a dictionary list from another list which having two keys with multiple values? 如何从字典列表中创建三个单独的值列表,其中每个字典都有三个键 - How to create three separate lists of values from a list of dictionaries where each dictionary has three keys 如何使用 Python 中的特定键从字典列表中创建字典 - How to create a dictionary from a list of dictionary using specific keys in Python 字典键的不同列表值 - Different list values for dictionary keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM