简体   繁体   English

使用 function 将元组列表转换为字典

[英]Converting a list of tuples to dictionary using a function

I am still learning python, so I tried to convert a list of tuples to a list of dictionaries using a function:我还在学习 python,所以我尝试使用 function 将元组列表转换为字典列表:

 query_result= [(3, 'base-certificate', 8, 2022), (2, 'case-certificate', 8, 2022), (3, 'standard-certificate', 8, 2022)]

I wrote a function that looks like this:我写了一个看起来像这样的 function:

 def convert_query_data_for_frontend(object, time):
            if time == "month":
                data = [
                    {
                        row[1]: row[0],
                        "date": [row[2], row[3]],
                    }
                    for row in object
                ]
                return data

The result of running this function was: convert_query_data_for_frontend(query_result, 'month')运行此 function 的结果是: convert_query_data_for_frontend(query_result, 'month')

[{'base-certificate': 3, 'date': [8, 2022]}, {'case-certificate': 2, 'date': [8, 2022]}, {'standard-certificate': 3, 'date': [8, 2022]}]

It is not yet the desired result should have only one date for different dates in this case the month could be september etc, instead it should return:还不是期望的结果应该只有一个日期用于不同的日期,在这种情况下,月份可能是 9 月等,而是应该返回:

[{'base-certificate': 3, 'case-certificate': 2, 'standard-certificate': 3, 'date': [8, 2022]}]

I thought just looping over the object and defining the dict would give me the desired result but didnt please what can I add or do in this case?我认为只是循环 object 并定义 dict 会给我想要的结果,但不希望在这种情况下我可以添加或做什么?

You have two things you're trying to do here:您在这里尝试做两件事:

  1. convert from tuples to dictionaries从元组转换为字典
  2. merge dictionaries合并字典

So far you have been successful in the first one but you're missing the second.到目前为止,您在第一个方面取得了成功,但您错过了第二个。

Starting with the output of your first function we can merge items with a common key using itertools.groupby and then utilize functools.reduce to combine the dictionaries we have grouped by date.从您的第一个 function 的 output 开始,我们可以使用itertools.groupby将项目与一个公共键合并,然后使用functools.reduce组合我们按日期分组的字典。 Additionally, since your data only has 1 date included I added some more dates to show off the grouping aspect此外,由于您的数据仅包含 1 个日期,因此我添加了更多日期以展示分组方面

from functools import reduce
from itertools import groupby


cert_dates = [
    {'base-certificate': 3, 'date': [8, 2022]}, {'case-certificate': 2, 'date': [8, 2022]}, {'standard-certificate': 3, 'date': [8, 2022]},
    {'base-certificate': 3, 'date': [9, 2022]}, {'case-certificate': 2, 'date': [9, 2022]}, {'standard-certificate': 3, 'date': [10, 2022]},
]
grouped_list = groupby(cert_dates, key=lambda x: x['date'])
output_records = []
for _, records in grouped_list:
    output_records.append(reduce(lambda x, y: {**x, **y}, records))
    
for x in output_records:
    print(x)
# {'base-certificate': 3, 'date': [8, 2022], 'case-certificate': 2, 'standard-certificate': 3}
# {'base-certificate': 3, 'date': [9, 2022], 'case-certificate': 2}
# {'standard-certificate': 3, 'date': [10, 2022]}

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

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