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

book
Challenge: Choosing Optimal Data Structures

Opgave

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.

Løsning

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))

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 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))

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt