Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen List Comprehensions and Generators | Section 2
Python - Music

Swipe um das Menü anzuzeigen

book
List Comprehensions and Generators

Think of list comprehensions as your musical score, where you can compose a list of notes in a single, elegant line. It's like writing a melody that flows seamlessly. With list comprehensions, you can create new lists by applying an expression to each item in an existing iterable, such as a list or a range.

Here's a simple example: Imagine you have a list of note durations in seconds, and you want to convert them to milliseconds. Instead of writing a loop, you can use a list comprehension:

1234
note_durations_sec = [0.5, 1.0, 1.5, 2.0] note_durations_ms = [duration * 1000 for duration in note_durations_sec] print("Note Durations in ms:", note_durations_ms)
copy

In this example, each note duration is multiplied by 1000 to convert it to milliseconds, all in one concise line. It's like hitting the perfect chord with minimal effort.

Now, let's talk about generators. Imagine you're creating an endless playlist of music that streams without taking up much space on your device. Generators are like that playlist—they allow you to iterate over data without storing it all in memory at once.

Generators are defined using functions and the yield keyword. Here's how you can create a simple generator that produces an infinite sequence of beats:

1234567891011
def infinite_beats(): beat = 1 while True: yield beat beat += 1 beat_generator = infinite_beats() # Let's play the first 5 beats for _ in range(5): print("Beat:", next(beat_generator))
copy

In this example, the infinite_beats generator function yields a new beat each time it's called. It's like having a drummer who never stops playing, providing you with a continuous rhythm.

List comprehensions and generators can work together harmoniously. For instance, you can use a generator to produce a large dataset and then apply a list comprehension to filter or transform it.

Aufgabe

Swipe to start coding

Complete the normalize_popularity function that normalizes a list of popularity scores. This normalization process scales the scores to a range between 0 and 1, making it easier to compare and analyze them.

Inputs:

  • popularity_scores: A list of integers representing the popularity scores to be normalized.

Steps:

  • Identify Minimum and Maximum Scores: Determine the minimum and maximum values from the popularity_scores list. These values will be used to scale the scores.

  • Normalize Scores: Use a list comprehension to iterate over each score in popularity_scores. For each score, apply the normalization formula

  • Normalization Formula: (score - min_score) / (max_score - min_score).

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

solution.py

py

main.py

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

book
List Comprehensions and Generators

Think of list comprehensions as your musical score, where you can compose a list of notes in a single, elegant line. It's like writing a melody that flows seamlessly. With list comprehensions, you can create new lists by applying an expression to each item in an existing iterable, such as a list or a range.

Here's a simple example: Imagine you have a list of note durations in seconds, and you want to convert them to milliseconds. Instead of writing a loop, you can use a list comprehension:

1234
note_durations_sec = [0.5, 1.0, 1.5, 2.0] note_durations_ms = [duration * 1000 for duration in note_durations_sec] print("Note Durations in ms:", note_durations_ms)
copy

In this example, each note duration is multiplied by 1000 to convert it to milliseconds, all in one concise line. It's like hitting the perfect chord with minimal effort.

Now, let's talk about generators. Imagine you're creating an endless playlist of music that streams without taking up much space on your device. Generators are like that playlist—they allow you to iterate over data without storing it all in memory at once.

Generators are defined using functions and the yield keyword. Here's how you can create a simple generator that produces an infinite sequence of beats:

1234567891011
def infinite_beats(): beat = 1 while True: yield beat beat += 1 beat_generator = infinite_beats() # Let's play the first 5 beats for _ in range(5): print("Beat:", next(beat_generator))
copy

In this example, the infinite_beats generator function yields a new beat each time it's called. It's like having a drummer who never stops playing, providing you with a continuous rhythm.

List comprehensions and generators can work together harmoniously. For instance, you can use a generator to produce a large dataset and then apply a list comprehension to filter or transform it.

Aufgabe

Swipe to start coding

Complete the normalize_popularity function that normalizes a list of popularity scores. This normalization process scales the scores to a range between 0 and 1, making it easier to compare and analyze them.

Inputs:

  • popularity_scores: A list of integers representing the popularity scores to be normalized.

Steps:

  • Identify Minimum and Maximum Scores: Determine the minimum and maximum values from the popularity_scores list. These values will be used to scale the scores.

  • Normalize Scores: Use a list comprehension to iterate over each score in popularity_scores. For each score, apply the normalization formula

  • Normalization Formula: (score - min_score) / (max_score - min_score).

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