Kursinhalt
Python - Sport
Dicts
Dictionaries in Python allow you to store data in a structured way, associating unique keys with specific values. This is particularly useful in data analytics, where you often need to organize and access data efficiently.
In Python, a dictionary is a collection of key-value pairs. Think of it as a sports team roster, where each player's name (key) is associated with their position or stats (value). Dictionaries are defined using curly braces {}
and key-value pairs are separated by colons :
.
Example:
python
You can access the values in a dictionary by using the keys. For example, to get LeBron James's points, you would use player_stats["LeBron James"]["points"]
. Additionally, dictionaries allow you to add new key-value pairs or update existing ones. For instance, if you want to update Anthony Davis's points, you can simply assign a new value: player_stats["Anthony Davis"]["points"] = 24
.
lebron_points = player_stats["LeBron James"]["points"] print(lebron_points) player_stats["Anthony Davis"]["points"] = 24 davis_points = player_stats["Anthony Davis"]["points"] print(davis_points)
You can use loops to iterate over dictionaries, which is useful for processing all key-value pairs. For example, to print each player's stats:
player_stats = { "Stephen Curry": {"points": 30, "rebounds": 5, "assists": 6}, "Kevin Durant": {"points": 28, "rebounds": 8, "assists": 4}, "Giannis Antetokounmpo": {"points": 27, "rebounds": 11, "assists": 5} } for player, stats in player_stats.items(): print(f"{player}: {stats}")
Swipe to start coding
Your goal is to complete group_score_by_matchup
function that processes a list of dictionaries, each representing a game, calculates the total score for each game, and groups these scores by a composite key "team1-team2-date".
Inputs:
games_info
: A list of dictionaries, where each dictionary contains information about a game, including keys like'team1'
,'team2'
,'date'
,'team1_score'
, and'team2_score'
.
Steps:
-
Initialize an Empty Dictionary: Create an empty dictionary
grouped_scores
to hold the grouped scores. -
Iterate Over Each Game: Use a for loop to iterate over each game dictionary in the
games_info
list. -
Calculate the Total Score: For each game, calculate the total score by adding
'team1_score'
and'team2_score'
. -
Create a Composite Key: Construct a composite key using the
'team1'
,'team2'
, and'score'
values. Format it as"team1-team2-date"
. -
Add to Dictionary: Add the total score to the
grouped_scores
dictionary using the composite key. -
Return the Dictionary: Ensure the function returns the
grouped_scores
dictionary with the grouped scores.
Lösung
Danke für Ihr Feedback!
Dicts
Dictionaries in Python allow you to store data in a structured way, associating unique keys with specific values. This is particularly useful in data analytics, where you often need to organize and access data efficiently.
In Python, a dictionary is a collection of key-value pairs. Think of it as a sports team roster, where each player's name (key) is associated with their position or stats (value). Dictionaries are defined using curly braces {}
and key-value pairs are separated by colons :
.
Example:
python
You can access the values in a dictionary by using the keys. For example, to get LeBron James's points, you would use player_stats["LeBron James"]["points"]
. Additionally, dictionaries allow you to add new key-value pairs or update existing ones. For instance, if you want to update Anthony Davis's points, you can simply assign a new value: player_stats["Anthony Davis"]["points"] = 24
.
lebron_points = player_stats["LeBron James"]["points"] print(lebron_points) player_stats["Anthony Davis"]["points"] = 24 davis_points = player_stats["Anthony Davis"]["points"] print(davis_points)
You can use loops to iterate over dictionaries, which is useful for processing all key-value pairs. For example, to print each player's stats:
player_stats = { "Stephen Curry": {"points": 30, "rebounds": 5, "assists": 6}, "Kevin Durant": {"points": 28, "rebounds": 8, "assists": 4}, "Giannis Antetokounmpo": {"points": 27, "rebounds": 11, "assists": 5} } for player, stats in player_stats.items(): print(f"{player}: {stats}")
Swipe to start coding
Your goal is to complete group_score_by_matchup
function that processes a list of dictionaries, each representing a game, calculates the total score for each game, and groups these scores by a composite key "team1-team2-date".
Inputs:
games_info
: A list of dictionaries, where each dictionary contains information about a game, including keys like'team1'
,'team2'
,'date'
,'team1_score'
, and'team2_score'
.
Steps:
-
Initialize an Empty Dictionary: Create an empty dictionary
grouped_scores
to hold the grouped scores. -
Iterate Over Each Game: Use a for loop to iterate over each game dictionary in the
games_info
list. -
Calculate the Total Score: For each game, calculate the total score by adding
'team1_score'
and'team2_score'
. -
Create a Composite Key: Construct a composite key using the
'team1'
,'team2'
, and'score'
values. Format it as"team1-team2-date"
. -
Add to Dictionary: Add the total score to the
grouped_scores
dictionary using the composite key. -
Return the Dictionary: Ensure the function returns the
grouped_scores
dictionary with the grouped scores.
Lösung
Danke für Ihr Feedback!