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

single

Challenge: Memory-Efficient Data Pipeline

Sveip for å vise menyen

Oppgave

Sveip for å begynne å kode

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øsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 5
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt