How do we change the symbols of the elements of the previous line in the matrix to the opposite provided?



  • See if there's at least one line in the matrix containing an element equal to zero and find her number. Signs of the elements of the previous line change to the opposite.

    I just know how to do the first part. How do we change the symbols of the previous line to the opposite?

    For example: [7, 2, 0] contains at least one zero element, then in the line [-5, -6, 2] signs must be changed.

    Please, how can I do that?

    matrix = [[-5, -6, 2], [7, 2, 0], [8, 4, -9]]
    

    row = 0
    for i in range(len(matrix)):
    for j in range(len(matrix[i])):
    if matrix[i][j] == 0:
    row = i
    print("The row", row," has at least one zero element.")



  • There you go.

    matrix = [[-5, -6, 2], [7, 2, 0], [8, 4, -9]]
    

    prev_arr = None

    for curr_arr in matrix:
    for elem in curr_arr:
    if elem == 0:
    if prev_arr:
    for i in range(len(prev_arr)):
    prev_arr[i] *= -1
    break
    prev_arr = curr_arr

    print str(matrix) # выведет [[5, 6, -2], [7, 2, 0], [8, 4, -9]]


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2