Structured arrays let you store different types of data together in one array. This helps when you want to keep related information organized, like a table with columns of different types.
Creating structured arrays in NumPy
import numpy as np # Define the data type with field names and types dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')] # Create an empty structured array structured_array = np.zeros(3, dtype=dtype) # Or create from a list of tuples data = [('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)] structured_array = np.array(data, dtype=dtype)
The dtype defines the structure: each field has a name and a data type.
Use 'U10' for a string of max length 10, 'i4' for 4-byte integer, 'f4' for 4-byte float.
import numpy as np dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')] # Empty array with 0 rows empty_array = np.zeros(0, dtype=dtype) print(empty_array)
import numpy as np dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')] # Array with one element one_element = np.array([('Diana', 28, 85.0)], dtype=dtype) print(one_element)
import numpy as np dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')] # Accessing fields students = np.array([('Eve', 21, 90.0), ('Frank', 24, 87.5)], dtype=dtype) print(students['name']) print(students['score'])
This program creates a structured array of students with name, age, and score. It prints the array, updates Bob's score, and prints the updated array.
import numpy as np # Define the structured data type student_dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')] # Create an array of students students = np.array([ ('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5) ], dtype=student_dtype) print("Before update:") print(students) # Update Bob's score for index, student in enumerate(students): if student['name'] == 'Bob': students[index]['score'] = 95.0 print("\nAfter update:") print(students)
Time complexity for accessing or updating a field is O(n) if you loop, but O(1) if you use vectorized operations.
Space complexity is efficient because all data is stored in a single NumPy array.
Common mistake: forgetting to specify dtype or mismatching data types causes errors.
Use structured arrays when you want fast access by field names and compact storage, instead of lists of dictionaries.
Structured arrays store mixed data types in one array with named fields.
You define the structure using a dtype with field names and types.
They help organize data like tables and allow easy access by column names.