Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Choosing Optimal Data Structures | Efficient Use of Data Structures
Optimization Techniques in Python

book
Challenge: Choosing Optimal Data Structures

Tarea

Swipe to start coding

Your task is to choose the most appropriate data structure (list, ndarray, set, or tuple) for each of the different scenarios below. Based on the nature of the data and the specific requirements for each collection, fill in the blanks (___) to create the correct data structures. Make sure to use the appropriate brackets for each structure, and if creating a NumPy array, initialize it based on a list.

  1. For monthly_sales, the data is numerical, and you will need to perform frequent calculations on it.
  2. user_ids, each ID should be unique, and you will need to frequently check if a certain ID is present.
  3. product should be a fixed (unchangeable) record.
  4. tasks should be an ordered collection and allow for frequent additions and removals.

Solución

import numpy as np

# Store monthly sales data (numerical data requiring calculations)
monthly_sales = np.array([320, 450, 500, 480, 350, 610, 720, 640, 510, 470, 420, 580])

# Keep track of unique user IDs who logged in during a day
user_ids = {1023, 2045, 1098, 2045, 3098, 1023}

# Define a product record
product = (2134, 'Wireless Mouse')

# Store an ordered collection of tasks where new tasks can be added or removed frequently
tasks = ['Send report', 'Review code', 'Update website', 'Fix bug']

print(type(monthly_sales))
print(type(user_ids))
print(type(product))
print(type(tasks))

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
import numpy as np

# Store monthly sales data (numerical data requiring calculations)
monthly_sales = ___320, 450, 500, 480, 350, 610, 720, 640, 510, 470, 420, 580

# Keep track of unique user IDs who logged in during a day
user_ids = ___1023, 2045, 1098, 2045, 3098, 1023

# Define a product record
product = ___2134, 'Wireless Mouse'

# Store an ordered collection of tasks where new tasks can be added or removed frequently
tasks = ___'Send report', 'Review code', 'Update website', 'Fix bug'

print(type(monthly_sales))
print(type(user_ids))
print(type(product))
print(type(tasks))
toggle bottom row
some-alt