Python factor delivery question

Asked 2 years ago, Updated 2 years ago, 87 views

defis_product_availability_matrix(matrix_a, matrix_b):
    row_a = [row for row in matrix_a]
    column_b = [column for column in zip(*matrix_b)]

    if(len(row_a) == len(column_b)):
        result = True
    else:
        result = False

    return result

matrix_x= [[2, 5], [1, 1]]
matrix_y = [[1, 1, 2], [2, 1, 1]]
matrix_z = [[2, 4], [5, 3], [1, 3]]
print(is_product_availability_matrix(matrix_y, matrix_z)) # # True
print(is_product_availability_matrix(matrix_z, matrix_x)) # # True
print(is_product_availability_matrix(matrix_z, matrix_w)) #  False
print(is_product_availability_matrix(matrix_x, matrix_x)) # # True

If you look at the 3rd print, there is a false error when you deliver a value that does not have matrix_w as a factor, and I want to get false as a return value. What should I do?

python matrix function

2022-09-22 10:17

1 Answers

NameError occurs because there are no variables.

NameError exception handling is typically available.

However, for simple actions such as questions,

print(is_product_availability_matrix(matrix_z, matrix_w if 'matrix_w' in locals() else []))

It can be handled as shown in .

Check whether the local variable exists or not, and forward an empty list.


2022-09-22 10:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.