合计字典值

huangapple go评论60阅读模式
英文:

Summing Dictinary Values

问题

我希望通过添加“amount”来简化列表,如果两个字典具有相同的“doc”和“account_name”键的值,例如在这种情况下,“account_name”:“Discount (Purchase)”和“doc”:“P.Inv-1”。

期望的结果应如下所示:

[
    {
      "account_name": "Rounded Off (Purchase)",
      "amount": 0.28,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 186.4,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    }
]

希望这对您有所帮助。

英文:

I have a list of dictionaries like this`

[
    {
      "account_name": "Rounded Off (Purchase)",
      "amount": 0.28,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 100,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 86.4,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    }
  ]`

I would like to simplify the list by adding the "amount" if two dictionaries have the same values for "doc" and "account_name" keys e.g. in this case "account_name" : "Discount (Purchase)" and "doc" : "P.Inv-1".

I can not think of a simple solution without using a lots of placeholder variables and and multiple loops over the list.

The expected result should look like

[
    {
      "account_name": "Rounded Off (Purchase)",
      "amount": 0.28,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 186.4,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    }
]

Any help is greatly appreciated. Thanks.

答案1

得分: 1

以下是翻译好的代码部分:

# 创建一个空列表,每当两个键不同时填充它。如果它们相同,更新amount-value。您可能需要决定"data_created"字段发生了什么,因为现在我根本不检查它(它保留第一个'data_created'值还是第二个?为什么?)
如果您没有提供可重现的示例我会编写如下内容

initial_dictionary_list = [
    {
      "account_name": "Rounded Off (Purchase)",
      "amount": 0.28,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 100,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 86.4,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T13:30:42.964203"
    }
  ]

from typing import Dict, List # 这些提供非常有用的类型提示

def process_dictionary_list(data: list[dict]) -> list[dict]:
    list_of_dictionaries_with_different_keys = {}
    for dictionary in dictionary_list:
        key = (dictionary['account_name'], dictionary['doc'])
        if key in list_of_dictionaries_with_different_keys:
            list_of_dictionaries_with_different_keys[key]['amount'] += dictionary['amount']
        else:
            list_of_dictionaries_with_different_keys[key] = dictionary.copy()
    return list(list_of_dictionaries_with_different_keys.values())

final_dictionary_list = process_dictionary_list(initial_dictionary_list)

print(final_dictionary_list)
# [{'account_name': 'Rounded Off (Purchase)', 'amount': 0.28, 'doc_date': '2023-04-05', 'doc': 'P.Inv.-1', 'date_created': '2023-04-05T15:30:42.964203'}, {'account_name': 'Discount (Purchase)', 'amount': 186.4, 'doc_date': '2023-04-05', 'doc': 'P.Inv.-1', 'date_created': '2023-04-05T15:30:42.964203'}]

请注意,已翻译的代码部分仅包括注释和Python代码。

英文:

It often helps to gradually think over such problems, and perhaps use simple pen and paper to understand how our variables will evolve over time.
Some little effort now can go a long way mid-long-term.

My approach would be to create an empty list, which is populated every time your two keys are different. If they are the same it updates the amount-value. You might want to decide what happens to "data_created" field, because now I do not examine it at all (Does it keep the first 'data_created' value or the second? Why?)
Since you don't provide a reproducible example I would write something as follows:

initial_dictionary_list = [
    {
      "account_name": "Rounded Off (Purchase)",
      "amount": 0.28,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 100,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T15:30:42.964203"
    },
    {
      "account_name": "Discount (Purchase)",
      "amount": 86.4,
      "doc_date": "2023-04-05",
      "doc": "P.Inv.-1",
      "date_created": "2023-04-05T13:30:42.964203"
    }
  ]

from typing import Dict, List # these provide very helpful type hints

def process_dictionary_list(data: list[dict]) -> list[dict]:
    list_of_dictionaries_with_different_keys = {}
    for dictionary in dictionary_list:
        key = (dictionary['account_name'], dictionary['doc'])
        if key in list_of_dictionaries_with_different_keys:
            list_of_dictionaries_with_different_keys[key]['amount'] += dictionary['amount']
        else:
            list_of_dictionaries_with_different_keys[key] = dictionary.copy()
    return list(list_of_dictionaries_with_different_keys.values())

final_dictionary_list = process_dictionary_list(initial_dictionary_list)

print(final_dictionary_list)
# [{'account_name': 'Rounded Off (Purchase)', 'amount': 0.28, 'doc_date': '2023-04-05', 'doc': 'P.Inv.-1', 'date_created': '2023-04-05T15:30:42.964203'}, {'account_name': 'Discount (Purchase)', 'amount': 186.4, 'doc_date': '2023-04-05', 'doc': 'P.Inv.-1', 'date_created': '2023-04-05T15:30:42.964203'}]

huangapple
  • 本文由 发表于 2023年4月6日 21:31:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定