简体   繁体   English

如何在 python 中添加或更新列表

[英]How to add or update list in python

I have the following list:我有以下列表:

DATA数据

data_set = [(2, 2021, '2300'),(1, 2021, '2500'),(12, 2020, '2400'),(11, 2020, '1500')]

I want to format this list into the following one,我想将此列表格式化为以下列表,

EXPECTED OUTPUT预期 OUTPUT

    [
        {
            "2021-01-01T00": [
                {
                    "2021-02-01T00:00:00": 2300
                },
                {
                    "2021-01-01T00:00:00": 2500
                }
            ]
        },
        {
            "2020-01-01T00": [
                {
                    "2020-12-01T00:00:00": 2400
                },
                {
                    "2020-11-01T00:00:00": 1500
                }
            ]
        }
    ]

I don't know how to format these two years in two objects.我不知道如何在两个对象中格式化这两年。 I tried hard but as I'm new to python so please help if you know the way to format this data.我很努力,但由于我是 python 的新手,所以如果您知道格式化此数据的方法,请提供帮助。

EDITED已编辑

SAMPLE CODE示例代码

result = []
for item in data_set:
    result.append({
        str(datetime.datetime(item[1], item[0], 1)): float(item[2])
    })

With above code I got the following output,使用上面的代码,我得到了以下 output,

    [
        {
            "2021-02-01 00:00:00": "2300"
        },
        {
            "2021-01-01 00:00:00": "2500"
        },
        {
            "2020-12-01 00:00:00": "2400"
        },
        {
            "2020-11-01 00:00:00": "1500"
        }
    ]

But I want the records of each year separate as I described above in the EXPECTED OUTPUT .但我希望每年的记录分开,正如我在EXPECTED OUTPUT中描述的那样。

This should work!这应该工作!

data_set = [(2, 2021, '2300'),(1, 2021, '2500'),(12, 2020, '2400'),(11, 2020, '1500')]
import datetime
final_dict = dict()
for item in data_set:

    year_date = str(datetime.datetime(item[1], 1, 1))
    full_date = str(datetime.datetime(item[1], item[0], 1))
    
    current_months_dict = final_dict.get(year_date, list())
    
    current_months_dict.append({full_date: item[2]})
    final_dict[year_date] = current_months_dict

final_list = []
for key, value in final_dict.items():
    final_list.append({key:value})
print(final_list)

Using groupby from itertools使用 itertools 中的groupby

from itertools import groupby
from datetime import datetime

m=[{" ".join(map(str,elem[0:2])):int(elem[2]) for elem in list(g)} for (k,g) in groupby(data_set,lambda x: x[1])]
#grouping is done based on the year 1.e x[1] first element
[{'2 2021': 2300, '1 2021': 2500}, {'12 2020': 2400, '11 2020': 1500}]

#using datetime module to parse date as required
n=[ {datetime.strptime(k,'%m %Y').strftime("%Y-%m-%dT%H:%M:%S"):v for k,v in item.items() } for item in m ]
[{'2021-02-01T00:00:00': 2300, '2021-01-01T00:00:00': 2500},
{'2020-12-01T00:00:00': 2400, '2020-11-01T00:00:00': 1500}]

#creating nested dict using minimum of existing keys
o=[{min(item.keys()):[{k:v} for k,v in item.items()]} for item in n]
[{'2021-01-01T00:00:00': [{'2021-02-01T00:00:00': 2300},
{'2021-01-01T00:00:00': 2500}]},
{'2020-11-01T00:00:00': [{'2020-12-01T00:00:00': 2400},
{'2020-11-01T00:00:00': 1500}]}]

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

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