CollegeStruggles
CollegeStruggles

Reputation: 21

How to iterate 2D arrays

I was assigned a homework assignment on 2D arrays, but we didn't have time to go over them. If anyone can try to walk me through it that would be much appreciated or direct me to a helpful source. I am not even sure where to begin so anything helps. Also if you could possibly walk through the first one that would be helpful. Thank you.

import numpy as np


def modify_2d_array(array):
    """
    Given a 2d numpy array iterate through the values and set the value equal to the row*column number

    :param array: 2d numpy array
    """
    pass


def sum_threshold_2d_array(array, threshold):
    """
    Iterate through each element of the 2d array using nested loops.
    Sum up the values that are greater than a threshold value given as an input parameter.

    :param array: a 2d array
    :param threshold: a threshold value (valid int or float)
    :return: sum total of values above the threshold
    """
    pass


def clipping_2d_array(array, threshold):
    """
    Iterate through each element of the 2d array using nested loops.
    Set the values greater than a threshold value equal to the threshold value given as an input parameter.

    (E.g. if the threshold is 1 and there is a value 1.5, set the value to 1 in the array)

    :param array: a 2d array
    :param threshold: a threshold value (valid int or float)
    """
    pass


def create_identity_matrix(n):
    """
    Create a nxn sized identity matrix. The return type should be a list or numpy ndarray

    For more info: https://en.wikipedia.org/wiki/Identity_matrix

    For this exercise you can use nested loops to construct your 2d array or find an applicable function in the numpy library.

    :param n:
    :return: nxn ndarray where the diagonal elements are 1 and nondiagonal elements are 0
    """

    pass


if __name__ == "__main__":
    my_example_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    print(sum_threshold_2d_array(my_example_array, 5))
    print(clipping_2d_array(my_example_array, 5))
    print(create_identity_matrix(5))

    # Modifies existing array
    modify_2d_array(my_example_array)
    print(my_example_array)

Upvotes: 0

Views: 223

Answers (2)

scottapotamus
scottapotamus

Reputation: 598

Just to add to Batcastle's answer, showing the iteration in the simplest way possible:

You are basically selecting a list from a list and then selecting an item from that list, so if your array was:

array = [['a','b','c'],['d','e','f'],['g','h','i']]

Here's how you'd get values from it:

# this would be equal to ['d','e','f']
x = array[1]

# this would be equal to 'h':
x = array[2][1]

# this would be equal to ['b','c']
x  = array[0][1:3]

Remember when iterating that it will always go from the first position up to but not including the last.

Hope that helps

Upvotes: 0

Batcastle
Batcastle

Reputation: 170

2D arrays are fairly simple to iterate, as a list of lists. A simple example on how to iterate:

my_list = [[1,2,3],[4,5,6],[7,8,9]]
for each in my_list:
    # each is going to be equal to each list in my_list, so we can iterate over it with another, nested, for loop
    for each1 in each:
        # do what you want to do to each value
        # in this example, I'm just printing
        print(each1)

Something similar to this should allow you to iterate over most 2D lists.

For more info, I suggest checking this out: https://www.geeksforgeeks.org/python-using-2d-arrays-lists-the-right-way/

Upvotes: 1

Related Questions