Abschnitt 2. Kapitel 5
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"),
]
- Define a class
Transactionwith__slots__ = ("transaction_id", "amount", "currency")and an__init__method that sets all three attributes. - Create a generator expression called
transaction_streamthat yields aTransactioninstance for each tuple inraw_transactions— do not use a list comprehension. - Using
array.arraywith type code"d"(double), create a variable calledusd_amountsthat contains theamountof every transaction wherecurrency == "USD". Consumetransaction_streamto build it. - Print
usd_amounts.
Lösung
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 5
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen