Python Array Dimensions

The 'array' module dimensions.

* The 'array' module does not inherently support multi-dimensional arrays like 'NumPy' does.

Python - Array Dimensions:

Array dimensions refer to the number of axes or dimensions present in an array and different types of arrays can have different numbers of dimensions.

You can simulate multi-dimensional arrays using nested lists or the 'array.array' type by creating arrays of arrays.

Here's a brief example of how you might simulate a two-dimensional 'array' using the array module:

py Copy Code
import array

# Create a 2D array (3x3) of integers
two_dim_array = [
    array.array("i", [0, 1, 2]),
    array.array("i", [3, 4, 5]),
    array.array("i", [6, 7, 8]),]

# Accessing elements
print(two_dim_array[0][1])
print(two_dim_array[1][2])

Explanation:

In this example, 'array.array('i', [0, 1, 2])' creates an array of integers with initial values '[0, 1, 2]'. Then, these arrays are placed into a list to create a 2D structure. You can access elements of the 2D array using typical list indexing.

Output:
1
5

Another example of how you can use the 'array' module to create a simple matrix program in Python:

PDF Copy Code
# Create a matrix with the specified number of rows and columns.
import array

def create_matrix(rows, cols):
    # Initialize an empty list to store the rows of the matrix
    matrix = []

    # Iterate over each row
    for _ in range(rows):
        # Create an array to represent a row, filled with zeros initially
        row = array.array("i", [0] * cols)
        # Append the row to the matrix
        matrix.append(row)
    return matrix

def print_matrix(matrix):
    """
    Print the matrix.
    """
    for row in matrix:
        print(row)

def main():
    # Number of rows and columns for the matrix
    rows = int(input("Enter the number of rows: "))
    cols = int(input("Enter the number of columns: "))

    # Create the matrix
    matrix = create_matrix(rows, cols)

    print("Enter the values for the matrix:")
    for i in range(rows):
        for j in range(cols):
            value = int(input(f"Enter value for matrix[{i}][{j}]: "))
            matrix[i][j] = value

    print("\nMatrix created:")
    print_matrix(matrix)

if __name__ == "__main__":
    main()

Explanation:

This program allows the user to input the number of rows and columns for the matrix after that it'll ask you to input values into the cells and then create a matrix using arrays.

Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter the values for the matrix:
Enter value for matrix[0][0]: 10
Enter value for matrix[0][1]: 20
Enter value for matrix[0][2]: 30
Enter value for matrix[1][0]: 40
Enter value for matrix[1][1]: 50
Enter value for matrix[1][2]: 60
Enter value for matrix[2][0]: 70
Enter value for matrix[2][1]: 80
Enter value for matrix[2][2]: 90

Matrix created:
array('i', [10, 20, 30])
array('i', [40, 50, 60])
array('i', [70, 80, 90])

Learn how matrix algorithms work in mathematical way.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.