Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Dicts | Section 1
Python - Sport
course content

Kursinhalt

Python - Sport

Python - Sport

1. Section 1
2. Section 2

book
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.

123456
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)
copy

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:

12345678
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}")
copy
Aufgabe

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:

  1. Initialize an Empty Dictionary: Create an empty dictionary grouped_scores to hold the grouped scores.

  2. Iterate Over Each Game: Use a for loop to iterate over each game dictionary in the games_info list.

  3. Calculate the Total Score: For each game, calculate the total score by adding 'team1_score' and 'team2_score'.

  4. Create a Composite Key: Construct a composite key using the 'team1', 'team2', and 'score' values. Format it as "team1-team2-date".

  5. Add to Dictionary: Add the total score to the grouped_scores dictionary using the composite key.

  6. Return the Dictionary: Ensure the function returns the grouped_scores dictionary with the grouped scores.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
toggle bottom row

book
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.

123456
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)
copy

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:

12345678
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}")
copy
Aufgabe

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:

  1. Initialize an Empty Dictionary: Create an empty dictionary grouped_scores to hold the grouped scores.

  2. Iterate Over Each Game: Use a for loop to iterate over each game dictionary in the games_info list.

  3. Calculate the Total Score: For each game, calculate the total score by adding 'team1_score' and 'team2_score'.

  4. Create a Composite Key: Construct a composite key using the 'team1', 'team2', and 'score' values. Format it as "team1-team2-date".

  5. Add to Dictionary: Add the total score to the grouped_scores dictionary using the composite key.

  6. Return the Dictionary: Ensure the function returns the grouped_scores dictionary with the grouped scores.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?
some-alt