Lecture-24 List-Array-Matrix General 2024-25
COMP101 Introduction to Programming - Lecture 24
Overview
Topic: Two-dimensional lists and arrays (2-D matrices) in Python
Multidimensional Structures
Types of Structures:
Lists, arrays, matrices can be categorized into:
1-dimensional
2-dimensional
Used to store data in elements with similar properties.
Python Limitations:
No built-in support for arrays; however, lists can be used as arrays.
For more extensive array handling, use third-party libraries like NumPy.
Terminology
List:
Generalized array form.
Ordered collection capable of holding multiple data types.
Array:
Specialized type of list.
Ordered collection restricted to elements of the same data type.
Matrix:
Specialized form of an array intended for mathematical and scientific applications.
1-D Structures
Definition:
Visualized as a single row collection or a linear series of elements.
Example:
A list dimensioned 1 x 7 has one row and seven columns.
2-D Structures
Definition:
Visualized as a grid of rows and columns.
Example:
A list dimensioned 4 x 7 encompasses four rows and seven columns.
Data is read in 'row-column order'.
Elements in 2-D Structures
Cell:
Intersections of rows and columns.
Each cell is referred to as an 'element'.
Total elements in a 2-D list could be calculated as the product of rows and columns (e.g., 4 x 7 = 28 elements).
Indexing in 2-D Lists
Index Access:
Every row and column is indexed.
Elements have two index values: the first indicating the row, the second indicating the column.
Example of Element Indexing:
First element: [0,0] corresponds to row[0], column[0].
Last element: [3,6] corresponds to row[3], column[6].
Element Access Pattern
Access Order:
Access is carried out in 'row-column order'.
The pattern for accessing elements can be illustrated as:
For example: 0,0 -> 0,1 -> 0,2 -> ..., leading to 3,6.
Addressing Mechanics
Row-Column Order:
Refers to accessing elements by the row index first followed by the column index.
Dimensions represented as (rows x columns).
For example, a dimension of 3 x 4 indicates 3 rows and 4 columns.
1-D vs 2-D Structures
1-D List Example:
meal_list = [soup, salad, fish, chips, peas, beans, cake, jelly]Stores data in a linear fashion.
2-D List Example:
meal_list = [[soup, salad], [fish, chips, peas, beans], [cake, jelly]]Represents a list of lists, clearly showing nested structures for rows.
Dimension Calculation:
Determined by the row with the most values (e.g., for 3 x 4).
Elements can be populated or null, indicating presence or absence of data.
List vs Array Comparison
List:
Can store diverse data types; not specialized for numerical operations.
Utilized for general purpose data storage and manipulation.
Array:
Stores homogeneous data types only.
Supports advanced computational functions essential for scientific computing.
Example: Division operation applied to all elements.
Python Arrays and NumPy
Python has Array Types:
Imported via the
arraymodule.Typically utilized for arithmetic operations.
Similar to lists but with strict type adherence.
NumPy:
Core library for scientific computing with matrix support.
Provides extensive functions for mathematical manipulations.
Import using
from numpy import *for comprehensive array handling.