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

Stryg for at vise menuen

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
Opgave

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øsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 5

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

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
Opgave

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øsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 5
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt