返回嵌套JSON文件中每个创建[年-月]的项目数

hwazgwia  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(96)

返回每个创建的项目数[年-月]。例如{ '2020- 04':29,“2020年5月”:二十四
我尝试返回每个创建的[年-月]的项目数。但我不知道如何连接创建的[年-月]与项目?
我的简单:

result = {}
for record in data_in:
    if record['created'][:7] not in result:
        result[record['created'][:7]] = {"created": 0}
    result[record["created"][:7]]["created"] += 1
        
result

我得到了,但我不知道我应该返回喜欢的项目?:这里的项目是指什么?“收入”和“支出”的总和还是什么?

{'2020-03': {'created': 1},
 '2020-04': {'created': 1},
 '2020-01': {'created': 3}}

但我需要:

# Return number of items per created[year-month] in data_in.
ex. {
     '2020-04': 29,
     '2020-05': 24
    }

我可以返回总数和跟踪期间,但如何连接创建与项目?
我尝试在data_in. ex. { '2020- 04':29,“2020年5月”:二十四
1.不允许在函数体之外进行更改。
1.不允许额外导入。
JSON文件

data_in =   [
    {
        "package": "FLEXIBLE",
        "created": "2020-03-10T00:00:00",
        "summary": [
            {
                "period": "2019-12",
                "documents": {
                    "incomes": 63,
                    "expenses": 13
                }
            },
            {
                "period": "2020-02",
                "documents": {
                    "incomes": 45,
                    "expenses": 81
                }
            }
        ]
    },
    {
        "package": "ENTERPRISE",
        "created": "2020-04-19T00:00:00",
        "summary": [
            {
                "period": "2020-01",
                "documents": {
                    "incomes": 15,
                    "expenses": 52
                }
            },
            {
                "period": "2020-02",
                "documents": {
                    "incomes": 76,
                    "expenses": 47
                }
            }
        ]
    },
    {
        'package': 'FLEXIBLE',
        'created': '2020-01-15T00:00:00',
        'summary': [
            {
                'period': '2020-03',
                'documents': {
                    'incomes': 39, 
                    'expenses': 48
                }
            },
            {
                'period': '2020-04', 
                'documents': {
                    'incomes': 76, 
                    'expenses': 20
                }
            }
        ]
    },
    
    {
        'package': 'INTERNAL',
        'created': '2020-01-07T00:00:00',
        'summary': [
            {
                'period': '2019-12',
                'documents': {
                    'incomes': 4, 
                    'expenses': 53
                }
            },
            {
                'period': '2020-01', 
                'documents': {
                    'incomes': 60, 
                    'expenses': 48
                }
            },
            {
                'period': '2020-02', 
                'documents': {
                    'incomes': 88, 
                    'expenses': 85
                }
            },
            {
                'period': '2020-03', 
                'documents': {
                    'incomes': 84, 
                    'expenses': 81
                }
            }
        ]
    },
    {
        'package': 'ENTERPRISE',
        'created': '2020-01-03T00:00:00',
        'summary': [
            {
                'period': '2020-04',
                'documents': {
                    'incomes': 27, 
                    'expenses': 13
                }
            }
        ]
    }]
x8diyxa7

x8diyxa71#

此方法将基于'created'日期对data_in中的对象进行计数。record['created'][:7]将从'created'字符串中提取YYYY-MM

new_dict = {}
for record in data_in:
    created = record['created'][:7]
    if created not in new_dict:
        new_dict[created] = 0
    new_dict[created] += 1
print(new_dict)
{'2020-03': 1, '2020-04': 1, '2020-01': 3}
dohp0rv5

dohp0rv52#

如果我正确地理解了您的问题,您可以修改代码,直接递增计数器,而不是将该值存储在字典中。

result = {}
for record in data_in:
    if record['created'][:7] not in result:
        result[record['created'][:7]] = 1
    else:
        result[record["created"][:7]] += 1

请注意,您的解决方案假定created日期始终为YYYY-MM-...
这是你要找的吗?

相关问题