英文:
Python: return nested dict of values
问题
def task_2(data_in):
'''
返回每个期间的文档数量(收入、支出、总计)。
例如:{
'2020-04': {
'incomes': 2480,
'expenses': 2695,
'total': 5175
},
'2020-05': {
'incomes': 2673,
'expenses': 2280,
'total': 4953
}
}
'''
period = []
for term in data_in:
for dict_val in term['summary']:
if dict_val['period'] in period:
continue
else:
period.append(dict_val['period'])
incomes = []
for term in data_in:
for dict_val in term['summary']:
incomes.append(dict_val['documents']['incomes'])
expenses = []
for term in data_in:
for dict_val in term['summary']:
expenses.append(dict_val['documents']['expenses'])
return {per: {"incomes": inc, "expenses": exp, "total": inc + exp} for per, inc, exp in
zip(period, incomes, expenses)}
英文:
I'm typing 3 list of loops, and I need return dict like in docstring. And it's wrong. How can I solve problem?
For each period you have to count incomes and expenses and total occurrence.
- Changes out of functions body are not allowed.
- Additional imports are not allowed.
- Data could have wholes in periods range ex. 2022-01,2022-03 (missing February), then we assume that item has 0 documents in period
Sample of dict looks like below:
[
{
"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-03-19T00:00:00",
"summary": [
{
"period": "2020-01",
"documents": {
"incomes": 15,
"expenses": 52
}
},
{
"period": "2020-02",
"documents": {
"incomes": 76,
"expenses": 47
}
}
]
}
]
Code for the task is here:
def task_2(data_in):
'''
Return number of documents per period (incomes, expenses, total).
ex. {
'2020-04': {
'incomes': 2480,
'expenses': 2695,
'total': 5175
},
'2020-05': {
'incomes': 2673,
'expenses': 2280,
'total': 4953
}
}
'''
period = []
for term in data_in:
for dict_val in term['summary']:
if dict_val['period'] in period:
continue
else:
period.append(dict_val['period'])
incomes = []
for term in data_in:
for dict_val in term['summary']:
incomes.append(dict_val['documents']['incomes'])
expenses = []
for term in data_in:
for dict_val in term['summary']:
expenses.append(dict_val['documents']['expenses'])
# dict_keys = ['incomes', 'expenses', 'total']
# list1 = [{k: v for k, v in zip(dict_keys, range(len(incomes)))}]
# return {k: v for k, v in zip(period, list1)}
return {per: {"incomes": inc, "expenses": exp, "total": inc + exp} for per, inc, exp in
zip(period, range(len(incomes)), range(len(expenses)))}
I'm typing loops in dict but returns:
{'2019-12': {'incomes': 1, 'expenses': 1, 'total': 2},
'2020-02': {'incomes': 2, 'expenses': 2, 'total': 4}}
答案1
得分: 3
你需要追踪期间并保存收入、支出和总计。
data = [
{
"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-03-19T00:00:00",
"summary": [
{
"period": "2020-01",
"documents": {
"incomes": 15,
"expenses": 52
}
},
{
"period": "2020-02",
"documents": {
"incomes": 76,
"expenses": 47
}
}
]
}
]
records = [record for records in data for record in records['summary']]
result = {}
template = {
"incomes": 0,
"expenses": 0,
"total": 0
}
for record in records:
period = record['period']
if period not in result:
result[period] = {"incomes": 0, "expenses": 0, "total": 0}
income = record['documents']['incomes']
expense = record['documents']['expenses']
total = income + expense
result[period]['incomes'] += income
result[period]['expenses'] += expense
result[period]['total'] += total
print(result)
# 输出 -> {'2019-12': {'incomes': 63, 'expenses': 13, 'total': 76}, '2020-02': {'incomes': 121, 'expenses': 128, 'total': 249}, '2020-01': {'incomes': 15, 'expenses': 52, 'total': 67}}
英文:
you need to track the period and save income, expenses, total
data = [
{
"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-03-19T00:00:00",
"summary": [
{
"period": "2020-01",
"documents": {
"incomes": 15,
"expenses": 52
}
},
{
"period": "2020-02",
"documents": {
"incomes": 76,
"expenses": 47
}
}
]}]
records = [record for records in data for record in records['summary'] ]
result = {}
template = {
"incomes": 0,
"expenses": 0,
"total": 0
}
for record in records:
period = record['period']
if period not in result:
result[period] = {"incomes": 0,"expenses": 0,"total": 0}
income = record['documents']['incomes']
expense = record['documents']['expenses']
total = income + expense
result[period]['incomes'] += income
result[period]['expenses'] += expense
result[period]['total'] += total
print(result)
# output -> {'2019-12': {'incomes': 63, 'expenses': 13, 'total': 76}, '2020-02': {'incomes': 121, 'expenses': 128, 'total': 249}, '2020-01': {'incomes': 15, 'expenses': 52, 'total': 67}}
答案2
得分: 3
你可以通过创建一个字典,以期间作为键,并以'incomes'、'expenses'和'total'的和作为值来解决这个问题。结构将如下所示:
result = dict(
'2019-12' = dict(
'incomes' = 0,
'expenses' = 0,
'total' = 0,
),
'2020-01' = dict(
'incomes' = 0,
'expenses' = 0,
'total' = 0,
)
)
但首先,你会将其实例化为空的dict
,然后在迭代数据时填充它。
result = dict()
# 或者简单地:result = {}
你将遍历data_in
中的每个term['summary']
中的每个term
,就像你已经在下面的代码中所做的那样:
for term in data_in:
for dict_val in term['summary']:
一旦你有要处理的数据,你需要检查当前dict_val
的期间是否已经存在于result
字典中。如果不存在,则使用初始值创建它:
if dict_val['period'] not in result:
result[dict_val['period']] = {'incomes': 0, 'expenses': 0, 'total': 0}
然后,你可以继续对当前dict_val
执行计算:
result[dict_val['period']]['incomes'] += dict_val['incomes']
result[dict_val['period']]['expenses'] += dict_val['expenses']
result[dict_val['period']]['total'] += dict_val['incomes'] + dict_val['expenses']
一旦迭代结束,你将得到你需要的数据。
英文:
You can solve that by creating a dict that has the period as key and a dict with the sums of incomes
, expenses
and total
as value for every key. The structure will be this:
result = dict(
'2019-12' = dict(
'incomes' = 0,
'expenses' = 0,
'total' = 0,
),
'2020-01' = dict(
'incomes' = 0,
'expenses' = 0,
'total' = 0,
)
)
But you will instantiate as an empty dict
at first, and will populate it as you iterate over the data.
result = dict()
# or simply: result = {}
You will iterate over every term['summary']
in every term
, in data_in
, as you are already doing with
for term in data_in:
for dict_val in term['summary']:
Once you have the data to be processed, you need to check if the period of the current dict_val
already exists in the result
dict. If not, create it with initial values:
if dict_val['period'] not in result:
result[dict_val['period']] = {'incomes': 0, 'expenses': 0, 'total': 0}
then, you proceed with doing the calculations for the current dict_val
:
result[dict_val['period']]['incomes'] += dict_val['incomes']
result[dict_val['period']]['expenses'] += dict_val['expenses']
result[dict_val['period']]['total'] += dict_val['incomes'] + dict_val['expenses']
Once the iteration is over, you will have your result
variable with the data you need
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论