我有一些......
[{"countryname": "Republic of Tunisia", "project_name": "TN: DTF Social Protection Reforms Support", "lendprojectcost": 5700000},
{"countryname": "Republic of Tunisia", "project_name": "Tunisia: Ecotourism and Conservation of Desert Biodiversity", "lendprojectcost": 9050000},
{"countryname": "Republic of Tunisia", "project_name": "Tunisia - Communications for policy reforms", "lendprojectcost": 600000},
{"countryname": "Republic of Tunisia", "project_name": "Tunisia - Governance, Opportunities and Jobs DPL", "lendprojectcost": 500000000}]
我想:
[{"countryname": "Republic of Tunisia", "project_name":all projects, "lendprojectcost":sum(..) }]
我怎么能这样做? 我使用了Python / Flask / MongoDB。
答案 0 :(得分:0)
作为熊猫极客,我会选择一个熊猫解决方案:从原始数据创建一个数据框(让我们称之为 1: ~/.vimrc
2: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/getscriptPlugin.vim
3: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/gzip.vim
4: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/logiPat.vim
5: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/manpager.vim
6: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/matchparen.vim
7: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/netrwPlugin.vim
8: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/rrhelper.vim
9: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/spellfile.vim
10: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/tarPlugin.vim
11: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/tohtml.vim
12: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/vimballPlugin.vim
13: /usr/local/Cellar/vim/8.0.0134/share/vim/vim80/plugin/zipPlugin.vim
),做所有必要的聚合,并构建另一个字典:
raw
答案 1 :(得分:0)
您可以首先使用collections.defaultdict
创建dict
对象,以便为每个"countryname"
创建唯一条目。然后将dict
转换为您想要的回复。
例如:
from collections import defaultdict
country_project, country_sum = defaultdict(list), defaultdict(int)
for country in country_list:
country_name = country["countryname"]
country_project[country_name].append(country['project_name'])
country_sum[country_name] += country['lendprojectcost']
# Map the `dict` to get desired result using list comprehension
new_list = [ {
'countryname': country,
'project_name': country_project[country],
'lendprojectcost': country_sum[country]} for country in country_project]
# OR, via using plain `for` loop as:
# new_list = []
# for country in country_project:
# new_list.append({
# 'countryname': country,
# 'project_name': country_project[country],
# 'lendprojectcost': country_sum[country]})
其中country_list
是问题中提到的原始列表,而my_list
保留的最终值是所需的结果:
[{
'countryname': 'Republic of Tunisia',
'project_name': [
'TN: DTF Social Protection Reforms Support',
'Tunisia: Ecotourism and Conservation of Desert Biodiversity',
'Tunisia - Communications for policy reforms',
'Tunisia - Governance, Opportunities and Jobs DPL'
],
'lendprojectcost': 515350000
}]
答案 2 :(得分:0)
这里的其他答案都很棒,但这似乎是reduce
的自然问题。您有一个列表,并且您正在尝试将列表中的元素与操作组合在一起,以便将列表大小“减少”到一个元素:
def update(x, y):
x["project_name"].append(y["project_name"])
x["lendprojectcost"] += y["lendprojectcost"]
return x
result = [reduce(update, list_of_dicts, {"countryname": "Republic of Tunisia",
"project_name": [],
"lendprojectcost": 0})]
奖金功能:无需导入