Good day everyone. I’m trying to count title of soccer team in list of dictionaries.
prev_match_stats = ({'league_title': league_title, 'match_date': match_date, 'home_team': home_team, 'score_ft': score_ft, 'home_team_score_ft': home_team_score_ft, 'away_team_score_ft': away_team_score_ft, 'score_ht': score_ht, 'corners': corners, 'away_team': away_team})
There are 10 (sometimes 20) games in list with data. So, home_team in list – looks like: {Team 1, … }, {Team 1, … }, {Team 2, … }, {Team 2, … }, {Team 1, … }, {Team 2, … }, {Team 2, … }, {Team 2, … }, {Team 2, … }.
I want a result of counting that looks like this: Team 1: 2; Team 2: 2; Team 1: 1, Team 2: 4.
for match in prev_match_stats: if '(N)' not in match['home_team'] and match['home_team'] == team1_name:
And i’m stuck. Should I use a collections or pandas? And how to do that?
List:
[{'league_title': 'VIE U19', 'match_date': '12/1/2021', 'home_team': 'Quang Nam U19', 'score_ft': '0-0', 'home_team_score_ft': '0', 'away_team_score_ft': '0', 'score_ht': '0-0', 'corners': '2-1', 'away_team': 'Hoang Anh Gia Lai U19'}, {'league_title': 'VIE U19', 'match_date': '10/1/2021', 'home_team': 'CLB TPHCM U19', 'score_ft': '0-3', 'home_team_score_ft': '0', 'away_team_score_ft': '3', 'score_ht': '0-0', 'corners': '2-10', 'away_team': 'Quang Nam U19'}, {'league_title': 'VIE U19', 'match_date': '8/6/2020', 'home_team': 'Quang Nam U19', 'score_ft': '0-4', 'home_team_score_ft': '0', 'away_team_score_ft': '4', 'score_ht': '0-1', 'corners': '1-1', 'away_team': 'Song Lam Nghe An U19'}, {'league_title': 'VIE U19', 'match_date': '5/6/2020', 'home_team': 'SHB Da Nang U19', 'score_ft': '3-1', 'home_team_score_ft': '3', 'away_team_score_ft': '1', 'score_ht': '2-0', 'corners': '2-5', 'away_team': 'Quang Nam U19'}, {'league_title': 'VIE U19', 'match_date': '3/6/2020', 'home_team': 'Huda Hue U19', 'score_ft': '0-2', 'home_team_score_ft': '0', 'away_team_score_ft': '2', 'score_ht': '0-2', 'corners': '3-6', 'away_team': 'Quang Nam U19'}, {'league_title': 'VIE U19', 'match_date': '1/6/2020', 'home_team': 'Quang Nam U19', 'score_ft': '1-3', 'home_team_score_ft': '1', 'away_team_score_ft': '3', 'score_ht': '0-2', 'corners': '4-1', 'away_team': 'Thanh Hoa U19'}, {'league_title': 'VIE U19', 'match_date': '10/3/2020', 'home_team': 'Song Lam Nghe An U19', 'score_ft': '1-0', 'home_team_score_ft': '1', 'away_team_score_ft': '0', 'score_ht': '0-0', 'corners': '', 'away_team': 'Quang Nam U19'}, {'league_title': 'VIE U19', 'match_date': '7/3/2020', 'home_team': 'Quang Nam U19', 'score_ft': '0-0', 'home_team_score_ft': '0', 'away_team_score_ft': '0', 'score_ht': '0-0', 'corners': '4-8', 'away_team': 'SHB Da Nang U19'}, {'league_title': 'VIE U19', 'match_date': '5/3/2020', 'home_team': 'Quang Nam U19', 'score_ft': '1-1', 'home_team_score_ft': '1', 'away_team_score_ft': '1', 'score_ht': '0-0', 'corners': '1-7', 'away_team': 'Huda Hue U19'}, {'league_title': 'VIE U19', 'match_date': '3/3/2020', 'home_team': 'Thanh Hoa U19', 'score_ft': '1-0', 'home_team_score_ft': '1', 'away_team_score_ft': '0', 'score_ht': '0-0', 'corners': '11-2', 'away_team': 'Quang Nam U19'}, {'league_title': 'INT CF', 'match_date': '24/2/2020', 'home_team': 'Quang Nam U19', 'score_ft': '2-1', 'home_team_score_ft': '2', 'away_team_score_ft': '1', 'score_ht': '1-1', 'corners': '2-2', 'away_team': 'Huda Hue U19'}, {'league_title': 'INT CF', 'match_date': '18/12/2019', 'home_team': 'Quang Nam U19', 'score_ft': '2-0', 'home_team_score_ft': '2', 'away_team_score_ft': '0', 'score_ht': '1-0', 'corners': '4-2', 'away_team': 'Khatoco Khanh Hoa U19'}]
Advertisement
Answer
Use itertools.groupby
:
>>> from itertools import groupby >>> [{name: len(list(group))} for name, group in groupby(team['home_team'] for team in teams)] [{'Quang Nam U19': 1}, {'CLB TPHCM U19': 1}, {'Quang Nam U19': 1}, {'SHB Da Nang U19': 1}, {'Huda Hue U19': 1}, {'Quang Nam U19': 1}, {'Song Lam Nghe An U19': 1}, {'Quang Nam U19': 2}, {'Thanh Hoa U19': 1}, {'Quang Nam U19': 2}]