簡體   English   中英

有人可以幫我將 **for 循環** 替換為 **while 循環** 我正在努力弄清楚嗎?

[英]Can someone help me replace the **for loop** to **while loop** I'm struggling to figure it out?

有人可以幫我將for 循環替換為while 循環嗎?

這個問題特別要求我們不要使用 for 循環。 這就是為什么我需要用while循環替換它

我在下面列出:

  1. 我的代碼
  2. 輸入output的樣品測試
  3. 我們必須遵守的條件:
 def matrix_equal(matrix1, matrix2):
    """
    -------------------------------------------------------
    Compares two matrices to see if they are equal - i.e. have the
    same contents in the same locations.
    Use: equal = matrix_equal(matrix1, matrix2)
    -------------------------------------------------------
    Parameters:
        matrix1 - the first matrix (2D list of ?)
        matrix2 - the second matrix (2D list of ?)
    Returns:
        equal - True if matrix1 and matrix2 are equal,
            False otherwise (boolean)
    ------------------------------------------------------
    """
    equal = True
    if((len(matrix1) != len(matrix2)) or (len(matrix1[0]) != len(matrix2[0]))):
        equal = False
    for x in range(len(matrix1)):
        if(equal == False):
            break
        for y in range(len(matrix1[0])):
            num1 = matrix1[x][y]
            num2 = matrix2[x][y]
            if(num1 != num2):
                equal = False
                break
    return equal

樣品測試:

First matrix:
      0    1    2
 0    c    a    t
 1    d    o    g
 2    b    i    g

Second matrix:
      0    1    2
 0    c    a    t
 1    d    o    g
 2    b    i    g

Equal matrices: True

我們必須遵守的條件:

1. should not call input in the function
2. should not call print in the function
3. should not have multiple returns

這應該可以解決您的問題,這是使用 while 循環的解決方案:

def matrix_equal(mat1,mat2):
  equal = True
  if(len(mat1[0]) == len(mat2[0]) and len(mat1) == len(mat2)):
    i = 0
    n = len(mat1[0])
    m = len(mat1)
    while(i < m):
      j = 0
      while(j < n):
        if(mat1[i][j] != mat2[i][j]):
          equal = False
          break
        j+=1
      if(equal==False):
        break
      i+=1
  else:
        equal = False
  return equal

改變

for x in range(len(matrix1)):

x = 0
while x < len(matrix1):
    x += 1

干杯!

您可以轉換:

for i in range(mat.shape[0]):
  {do stuff...}

進入

i = 0
while i < mat.shape[0]:
  {do stuff...}
  # increment i with 1
  i += 1

所以在這里你會得到:

def mat_eq_while(matrix1, matrix2):
  i = 0
  j = 0
  equal = True
  if(not (mat1.shape == mat2.shape) ):
      equal = False
  while i < mat1.shape[0]:
      if(equal == False):
          break
      while j < mat1.shape[1]:
          num1 = matrix1[i, j]
          num2 = matrix2[i, j]
          if(num1 != num2):
              equal = False
              break
          j += 1
      i += 1
  return equal

測試它

import numpy as np
mat1 = np.matrix(range(9)).reshape(3,3)
mat2 = np.matrix(range(1, 10)).reshape(3,3)

print( mat_eq_while(mat1, mat1) )
print( mat_eq_while(mat1, mat2) )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM