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

Conteúdo do Curso

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
Tarefa

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 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
Tarefa

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt