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

single

Challenge: Memory-Efficient Data Pipeline

Scorri per mostrare il menu

Compito

Scorri per iniziare a programmare

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt