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

Stryg for at vise menuen

book
Loop Techniques

Nested loops are loops within loops, particularly useful when dealing with multi-dimensional data, such as a list of lists. Imagine you have a dataset representing scores from multiple games, where each game has scores for both teams.

123456789101112
games_scores = [ [24, 21], # Game 1: Team A vs Team B [30, 27], # Game 2: Team A vs Team B [17, 20] # Game 3: Team A vs Team B ] # Calculate the total score for each game for game in games_scores: total_score = 0 for score in game: total_score += score print(f"Total score for the game: {total_score}")
copy

In this example, the outer loop iterates over each game, while the inner loop iterates over the scores within that game to calculate the total score.

List comprehensions provide a concise way to create lists, often more readable and efficient than traditional loops. You can use them to perform operations on each element of a list and generate a new list. Suppose you want to calculate the average score for each game from the games_scores dataset:

12345678
games_scores = [ [24, 21], [30, 27], [17, 20] ] average_scores = [sum(game) / len(game) for game in games_scores] print(f"Average scores for each game: {average_scores}")
copy

This list comprehension iterates over each game, calculates the average score, and stores the result in a new list.

You can also use nested loops within list comprehensions to perform more complex operations. For example, if you want to flatten a list of lists into a single list of scores:

python
Opgave

Swipe to start coding

Complete a function filter_high_scoring_games that filters a list of game records to find all games where the total score exceeded a given threshold. Use list comprehensions to achieve this efficiently.

Inputs:

  1. games: A list of dictionaries, each representing a game with keys such as away_score and home_score.
  2. score_threshold: An integer representing the score threshold.

Steps:

  1. Filter Games: Use a list comprehension to iterate over the list of games and filter out those where the combined score of away_score and home_score exceeds score_threshold.
  2. Return Filtered List: Return the list of filtered games.

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 2. Kapitel 3

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
Loop Techniques

Nested loops are loops within loops, particularly useful when dealing with multi-dimensional data, such as a list of lists. Imagine you have a dataset representing scores from multiple games, where each game has scores for both teams.

123456789101112
games_scores = [ [24, 21], # Game 1: Team A vs Team B [30, 27], # Game 2: Team A vs Team B [17, 20] # Game 3: Team A vs Team B ] # Calculate the total score for each game for game in games_scores: total_score = 0 for score in game: total_score += score print(f"Total score for the game: {total_score}")
copy

In this example, the outer loop iterates over each game, while the inner loop iterates over the scores within that game to calculate the total score.

List comprehensions provide a concise way to create lists, often more readable and efficient than traditional loops. You can use them to perform operations on each element of a list and generate a new list. Suppose you want to calculate the average score for each game from the games_scores dataset:

12345678
games_scores = [ [24, 21], [30, 27], [17, 20] ] average_scores = [sum(game) / len(game) for game in games_scores] print(f"Average scores for each game: {average_scores}")
copy

This list comprehension iterates over each game, calculates the average score, and stores the result in a new list.

You can also use nested loops within list comprehensions to perform more complex operations. For example, if you want to flatten a list of lists into a single list of scores:

python
Opgave

Swipe to start coding

Complete a function filter_high_scoring_games that filters a list of game records to find all games where the total score exceeded a given threshold. Use list comprehensions to achieve this efficiently.

Inputs:

  1. games: A list of dictionaries, each representing a game with keys such as away_score and home_score.
  2. score_threshold: An integer representing the score threshold.

Steps:

  1. Filter Games: Use a list comprehension to iterate over the list of games and filter out those where the combined score of away_score and home_score exceeds score_threshold.
  2. Return Filtered List: Return the list of filtered games.

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 2. Kapitel 3
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