基于创建日期从嵌套JSON对象列表中返回对象数[已关闭]

7cjasjjr  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(99)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
这篇文章是编辑和提交审查2天前。
Improve this question
我有一个JSON对象列表,希望返回一个字典,其中包含基于created[year-month]的对象计数,其中created[year-month]是键,count是值。
我试过:

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}}

但我想要的结果是:

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

我知道我错过了一些东西,但不知道是什么。有人能帮助重构代码,或提供一个更好的方法来获得所需的输出吗?

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
                }
            }
        ]
    }]
zxlwwiss

zxlwwiss1#

此方法将基于'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}
ohtdti5x

ohtdti5x2#

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

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-...
这是你要找的吗?

相关问题