Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Memory-Efficient Data Pipeline | Writing Memory-Efficient Code
Python Memory Management
Секція 2. Розділ 5
single

single

Challenge: Memory-Efficient Data Pipeline

Свайпніть щоб показати меню

Завдання

Проведіть, щоб почати кодувати

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.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt