Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Set operations on structured data
📖 Scenario: You work in a store that tracks products by their name and price. You want to find which products are unique to each store's list and which products are common.
🎯 Goal: Build a program that uses numpy structured arrays to find the intersection, union, and difference of two product lists.
📋 What You'll Learn
Create two numpy structured arrays with product data
Define a configuration variable for the data type
Use numpy set operations on structured arrays
Print the results clearly
💡 Why This Matters
🌍 Real World
Stores and businesses often compare product lists from different branches or suppliers to manage inventory and pricing.
💼 Career
Data scientists use structured arrays and set operations to clean, compare, and analyze datasets with multiple fields efficiently.
Progress0 / 4 steps
1
DATA SETUP: Create two numpy structured arrays
Import numpy as np. Create a numpy structured array called store1 with these exact entries: ('apple', 0.50), ('banana', 0.30), ('orange', 0.80). Create another numpy structured array called store2 with these exact entries: ('banana', 0.30), ('kiwi', 1.00), ('apple', 0.50). Use a data type with fields 'name' as 'U10' and 'price' as 'f4'.
NumPy
Hint
Use np.array with a list of tuples and specify dtype for structured arrays.
2
CONFIGURATION: Define the data type variable
Create a variable called dtype and set it to a list of tuples with fields ('name', 'U10') and ('price', 'f4').
NumPy
Hint
Set dtype exactly as a list of tuples with field names and types.
3
CORE LOGIC: Use numpy set operations on structured arrays
Use np.intersect1d with store1 and store2 to find common products and save to common. Use np.union1d to find all unique products and save to all_products. Use np.setdiff1d with store1 and store2 to find products only in store1 and save to only_store1.
NumPy
Hint
Use np.intersect1d, np.union1d, and np.setdiff1d with the two arrays.
4
OUTPUT: Print the results clearly
Print the variables common, all_products, and only_store1 each on a separate line with labels: "Common products:", "All products:", and "Only in store1:" respectively.
NumPy
Hint
Use print with labels and variables on separate lines.
Practice
(1/5)
1. What does the numpy.intersect1d function do when applied to two structured arrays?
easy
A. Finds rows present only in the second array
B. Combines all rows from both arrays without duplicates
C. Finds the common rows present in both arrays
D. Finds rows present only in the first array
Solution
Step 1: Understand intersect1d purpose
numpy.intersect1d returns elements common to both input arrays.
Step 2: Apply to structured arrays
For structured arrays, it compares rows and returns those present in both arrays.
Final Answer:
Finds the common rows present in both arrays -> Option C
Quick Check:
Intersection = common rows [OK]
Hint: Intersect means common elements only [OK]
Common Mistakes:
Confusing intersect1d with union1d
Thinking it returns unique rows from one array only
Assuming it returns rows exclusive to one array
2. Which of the following is the correct syntax to find the union of two structured numpy arrays a and b?
easy
A. numpy.union(a | b)
B. numpy.union(a, b)
C. numpy.setunion(a, b)
D. numpy.union1d(a, b)
Solution
Step 1: Recall numpy union function
The correct function to find union is numpy.union1d.
Step 2: Check syntax correctness
The syntax is numpy.union1d(a, b) with two arguments.
Final Answer:
numpy.union1d(a, b) -> Option D
Quick Check:
Use union1d for union operation [OK]
Hint: Use union1d, not union or setunion [OK]
Common Mistakes:
Using nonexistent functions like union or setunion
Passing arguments incorrectly with bitwise operators