我想按年份和城市分組,同時在輸出中累積日期和點數,我是 python 新手,不知道如何去做,你有什么想法嗎?我提取了串列 list_year 和 list_city,不確定這是否有用?謝謝
list_pts = [
{'city' : 'Madrid', 'year' : '2017', 'date' : '05/07/2017', 'pts' : 7},
{'city' : 'Madrid', 'year' : '2017', 'date' : '14/11/2017', 'pts' : 5},
{'city' : 'Londres', 'year' : '2018', 'date' : '25/02/2018', 'pts' : 5},
{'city' : 'Paris', 'year' : '2019', 'date' : '17/04/2019', 'pts' : 4},
{'city' : 'Londres', 'year' : '2019', 'date' : '15/06/2019', 'pts' : 8},
{'city' : 'Paris', 'year' : '2019', 'date' : '21/08/2019', 'pts' : 8},
{'city' : 'Londres', 'year' : '2019', 'date' : '04/12/2019', 'pts' : 2}]
list_year = ['2017', '2018', '2019']
list_city = ['Paris', 'Madrid', 'Londres']
output =
[{'year' : '2017', 'city' : 'Madrid', 'date' : ['05/07/2017', '14/11/2017'], 'pts' :[5, 7]},
{'year' : '2018', 'city' : 'Londres', 'date' : ['25/02/2018'], 'pts' :[5]},
{'year' : '2019', 'city' : 'Londres', 'date' : ['15/06/2019', '04/12/2019'], 'pts' :[8, 2]},
{'year' : '2019', 'city' : 'Paris', 'date' : ['17/04/2019', '21/08/2019'], 'pts' :[4, 8]}]
uj5u.com熱心網友回復:
您可以groupby()將 dicts 與itemgetter()以下組合使用:
from itertools import groupby
from operator import itemgetter
list_pts = [
{'city': 'Madrid', 'year': '2017', 'date': '05/07/2017', 'pts': 7},
{'city': 'Madrid', 'year': '2017', 'date': '14/11/2017', 'pts': 5},
{'city': 'Londres', 'year': '2018', 'date': '25/02/2018', 'pts': 5},
{'city': 'Paris', 'year': '2019', 'date': '17/04/2019', 'pts' : 4},
{'city': 'Londres', 'year': '2019', 'date': '15/06/2019', 'pts': 8},
{'city': 'Paris', 'year': '2019', 'date': '21/08/2019', 'pts': 8},
{'city': 'Londres', 'year': '2019', 'date': '04/12/2019', 'pts': 2}
]
city_year_getter = itemgetter('city', 'year')
date_pts_getter = itemgetter('date', 'pts')
result = []
for (city, year), objs in groupby(sorted(list_pts, key=city_year_getter),
city_year_getter):
dates, ptss = zip(*map(date_pts_getter, objs))
result.append({
'city': city,
'year': year,
'date': list(dates),
'pts': list(ptss)
})
uj5u.com熱心網友回復:
基于對collections.defaultdict值進行分組的使用,一種方法是執行以下操作:
from collections import defaultdict
from operator import itemgetter
import pprint
list_pts = [
{'city': 'Madrid', 'year': '2017', 'date': '05/07/2017', 'pts': 7},
{'city': 'Madrid', 'year': '2017', 'date': '14/11/2017', 'pts': 5},
{'city': 'Londres', 'year': '2018', 'date': '25/02/2018', 'pts': 5},
{'city': 'Paris', 'year': '2019', 'date': '17/04/2019', 'pts': 4},
{'city': 'Londres', 'year': '2019', 'date': '15/06/2019', 'pts': 8},
{'city': 'Paris', 'year': '2019', 'date': '21/08/2019', 'pts': 8},
{'city': 'Londres', 'year': '2019', 'date': '04/12/2019', 'pts': 2}]
# function for extracting city and year (to be used as a grouping key)
city_and_year = itemgetter("city", "year")
# function for extracting dates and points
date_and_points = itemgetter("date", "pts")
# group by key (city, year) by using a defaultdict
res = defaultdict(list)
for record in list_pts:
res[city_and_year(record)].append(date_and_points(record))
# transform to the desired format
result = []
for (city, year), values in res.items():
dates, points = zip(*values)
result.append({"city": city, "year": year, "dates": list(dates), "pts": list(points)})
# use pprint to nicely print the output
pprint.pprint(result)
輸出
[{'city': 'Madrid',
'dates': ['05/07/2017', '14/11/2017'],
'pts': [7, 5],
'year': '2017'},
{'city': 'Londres', 'dates': ['25/02/2018'], 'pts': [5], 'year': '2018'},
{'city': 'Paris',
'dates': ['17/04/2019', '21/08/2019'],
'pts': [4, 8],
'year': '2019'},
{'city': 'Londres',
'dates': ['15/06/2019', '04/12/2019'],
'pts': [8, 2],
'year': '2019'}]
uj5u.com熱心網友回復:
list_pts = [
{'city': 'Madrid', 'year': '2017', 'date': '05/07/2017', 'pts': 7},
{'city': 'Madrid', 'year': '2017', 'date': '14/11/2017', 'pts': 5},
{'city': 'Londres', 'year': '2018', 'date': '25/02/2018', 'pts': 5},
{'city': 'Paris', 'year': '2019', 'date': '17/04/2019', 'pts': 4},
{'city': 'Londres', 'year': '2019', 'date': '15/06/2019', 'pts': 8},
{'city': 'Paris', 'year': '2019', 'date': '21/08/2019', 'pts': 8},
{'city': 'Londres', 'year': '2019', 'date': '04/12/2019', 'pts': 2}]
group_by = {}
for pt in list_pts:
d = group_by.setdefault((pt['year'], pt['city']), {})
for k, v in pt.items():
if k not in {'year', 'city'}:
d.setdefault(k, []).append(v)
output = []
for (year, city), rest in group_by.items():
output.append({'year': year, 'city': city} | rest)
對于 3.9 之前的 Python,請參閱有關字典合并的答案。
這是一個使用這(year, city)對作為鍵(可以是 Python 中的元組)并將所有其他專案收集到串列中的問題。從group_by變成的轉變output只是一種裝飾性的東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/318286.html
