-3

I need to check if the numbers in gradescale is in my NxM matrix as a numpy array, if example the number 8 is in my matrix, I would like to append the number to a empty list and the row number to another list So how do i check if the number in my matrix isn't in gradescale, i have tried different types of loops, but they dont work.

 wrongNumber = []
 Rows = []
 gradeScale = np.array([-3,0,2,4,7,10,12])
 if there is a number i matrix which is not i gradeScale
     wrongNumber.append[number]
     Rows.append[rownumber]
print("the grade {} in line {} is out of range",format(wrongNumber), 
format(Rows))

2 Answers2

1

You can use numpy.ndarray.shape to go through your rows.

for row in range(matrix.shape[0]):
    for x in matrix[row]:
        if x not in gradeScale:
            wrongNumber.append(x)
            Rows.append(row)

In addition, you do not use format correctly. Your print statement should be

print("The grade {} in line {} is out of range".format(wrongNumber, Rows))

The following post has some more information on formatting String formatting in Python .

Example

import numpy as np

wrongNumber = []
Rows = []

matrix = np.array([[1,2],[3,4],[5,6],[7,8]])
gradeScale = [1,3,4,5,8]

for row in range(matrix.shape[0]):
    for x in matrix[row]:
        if x not in gradeScale:
            wrongNumber.append(x)
            Rows.append(row)

print("The grades {} in lines {} (respectively) are out of range.".format(wrongNumber, Rows))

Output

The grades [2, 6, 7] in lines [0, 2, 3] (respectively) are out of range
yakobyd
  • 572
  • 4
  • 12
0

Probably a for loop with enumerate() is what you are looking for.

Example:

for rowNumber, number in enumerate(matrix)
    if number not in gradeScale:
        wrongNumber.append[number]
        Rows.append[rowNumber]
Ash Sharma
  • 470
  • 3
  • 18
eriknayan
  • 1
  • 3