Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Memory-Efficient Data Pipeline | Writing Memory-Efficient Code
Python Memory Management
Abschnitt 2. Kapitel 5
single

single

Challenge: Memory-Efficient Data Pipeline

Swipe um das Menü anzuzeigen

Aufgabe

Wischen, um mit dem Codieren zu beginnen

You are building a memory-efficient pipeline that processes a large sequence of transaction records. Your goal is to use the right data structures to minimize memory usage throughout.

You are given the following list of raw transaction tuples:

raw_transactions = [
    (1, 4500.0, "USD"),
    (2, 1200.5, "EUR"),
    (3, 8900.0, "USD"),
    (4, 300.75, "EUR"),
    (5, 15000.0, "USD"),
]
  1. Define a class Transaction with __slots__ = ("transaction_id", "amount", "currency") and an __init__ method that sets all three attributes.
  2. Create a generator expression called transaction_stream that yields a Transaction instance for each tuple in raw_transactions — do not use a list comprehension.
  3. Using array.array with type code "d" (double), create a variable called usd_amounts that contains the amount of every transaction where currency == "USD". Consume transaction_stream to build it.
  4. Print usd_amounts.

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 5
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

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

some-alt