简体   繁体   English

在 Python 中将元组列表转换为字典列表

[英]converting a list of tuples to list of dictionary in Python

I am trying to convert a list of tuple to a list of dictionary with a data that looks like this:我正在尝试将元组列表转换为字典列表,其数据如下所示:

data =[
    ('base-certificate', 60, 3, 2022), 
    ('case-certificate', 1, 3, 2022), 
    ('standard-certificate', 7, 3, 2022),
    ('base-certificate', 50, 4, 2022), 
    ('case-certificate', 80, 4, 2022), 
    ('standard-certificate', 10, 4, 2022)
]

The last two numbers are dates representing month and year, this means that there can also be data for April(4), May(5), etc. and it would be unique.最后两个数字是代表月份和年份的日期,这意味着也可以有 April(4)、May(5) 等的数据,并且是唯一的。 so to create a date object in javascript these two numbers are needed preferably in a list [3, 2022].因此,要在 javascript 中创建日期对象,最好在列表 [3, 2022] 中需要这两个数字。

I would like an output that looks like this:我想要一个如下所示的输出:

[
    {x:[3, 2022], base: 60, case:1, standard: 7}, 
    {x:[4, 2022], base: 50, case:80, standard: 10} 
]

I tried using the defaultdict from the collection library:我尝试使用集合库中的 defaultdict:

from collections import defaultdict
data=defaultdict(dict) 

for a, b, c, d in se:
    if "x" not in data[c, d]:
        data[c, d]['x'] = [c,d]
    data[c, d][a] = b
        
print(data)  

The result of the code was:代码的结果是:

defaultdict(<class 'dict'>, {(3, 2022): {'x': [3, 2022], 'base-certificate': 60, 'case-certificate': 1, 'standard-certificate': 7}, (4, 2022): {'x': [4, 2022], 'base-certificate': 50, 'case-certificate': 80, 'standard-certificate': 10}})

it was close but am missing something.它很接近,但我错过了一些东西。 Please what is the best approach in this case?请问在这种情况下最好的方法是什么? and what am I am doing wrong, Thank you for your kind response.我做错了什么,谢谢您的友好回复。

Here's a possible solution using itertools.groupby .这是使用itertools.groupby的可能解决方案。 If the dates are sorted you can use groupby directly on the data, if not, you should sort by date first.如果日期已排序,您可以直接在数据上使用groupby ,如果没有,则应先按日期排序。

from itertools import groupby

result = []
for key, group in groupby(data, lambda x: (x[2], x[3])):
    r = {'x': key}
    for a, b, _, _ in group:
        r[a.partition('-')[0]] = b
    result.append(r)

print(result)

Output:输出:

[{'x': (3, 2022), 'base': 60, 'case': 1, 'standard': 7}, {'x': (4, 2022), 'base': 50, 'case': 80, 'standard': 10}]

Try:尝试:

data = [
    ("base-certificate", 60, 3, 2022),
    ("case-certificate", 1, 3, 2022),
    ("standard-certificate", 7, 3, 2022),
    ("base-certificate", 50, 4, 2022),
    ("case-certificate", 80, 4, 2022),
    ("standard-certificate", 10, 4, 2022),
]

out = {}
for name, val, month, year in data:
    out.setdefault((month, year), {}).update(
        {"x": [month, year], name.split("-")[0]: val}
    )

print(list(out.values()))

Prints:印刷:

[
    {"x": [3, 2022], "base": 60, "case": 1, "standard": 7},
    {"x": [4, 2022], "base": 50, "case": 80, "standard": 10},
]

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

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