鱼C论坛

 找回密码
 立即注册
查看: 106|回复: 8

[已解决]为什么列表VII的最后一题会报错

[复制链接]
发表于 2024-4-14 02:18:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下这段代码
  1. matrix = [[1, 2, 3, 4],
  2.           [5, 6, 7, 8],
  3.           [9, 10, 11, 12]]
  4. rows = len(matrix)
  5. cols = len(matrix[0])
  6. top = 0
  7. right = cols - 1
  8. left = 0
  9. bottom = rows - 1
  10. result = []
  11. while left <= right and top <= bottom:
  12.     for col in range(left,right + 1):
  13.         result.append(matrix[top][col])
  14.     for row in range(top + 1,bottom + 1):
  15.         result.append(matrix[row][right])
  16.     if left < right and top < bottom:   
  17.         for col in range(right - 1,left,-1):
  18.             result.append(matrix[bottom][col])
  19.         for row in range(bottom - 1,top - 1):
  20.             result.append(matrix[row][left])
  21.     left += 1
  22.     top -= 1
  23.     bottom += 1
  24.     right -= 1

  25. print(result)
复制代码

会报如下错误,为什么
  1. Traceback (most recent call last):
  2.   File "C:/Users/daish/Desktop/1.py", line 15, in <module>
  3.     result.append(matrix[row][right])
  4. IndexError: list index out of range
复制代码
最佳答案
2024-4-15 10:02:09
本帖最后由 jackz007 于 2024-4-15 10:08 编辑
  1. while left <= right and top <= bottom:
  2. . . . . . .
  3.     left += 1          # left < right 小的增大,循环有终点 -【逻辑正确】
  4.     right -= 1         # left < right 大的减小,循环有终点 -【逻辑正确】
  5.     top -= 1           # top < bottom 小的减小,循环无终点 -【逻辑错误】
  6.     bottom += 1        # top < bottom 大的增大,循环无终点 -【逻辑错误】
复制代码

        【修改】:
  1.     top += 1           # top < bottom 小的增大,循环有终点 -【逻辑正确】
  2.     bottom -= 1        # top < bottom 大的减小,循环有终点 -【逻辑正确】
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-14 02:18:56 | 显示全部楼层
这段代码报错是因为在定义列表`matrix`时,使用了两个逗号而没有填入具体的元素。在Python中,逗号用来分隔列表中的元素,而不是表示空元素。所以会导致语法错误。

如果你想要创建一个空的矩阵,可以直接使用两个方括号`[]`表示空列表即可,如下所示:

  1. matrix = [[],
  2.           [],
  3.           ]
复制代码


这样就创建了一个包含3行0列的空矩阵。如果你想要根据实际需要填充矩阵的元素,可以在每一行的方括号内填入具体的值,例如:

  1. matrix = [[1, 2, 3],
  2.           [4, 5, 6],
  3.           [7, 8, 9]
  4.           ]
复制代码


希望能帮到你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-14 02:20:42 | 显示全部楼层
FishC 发表于 2024-4-14 02:18
这段代码报错是因为在定义列表`matrix`时,使用了两个逗号而没有填入具体的元素。在Python中,逗号用来分隔 ...

这是一个4*3的列表啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-14 02:21:01 | 显示全部楼层
captaindai 发表于 2024-04-14 02:20
这是一个4*3的列表啊


非常抱歉,看漏了您代码中的具体矩阵内容。根据您提供的信息,这是一个4行3列的列表。在定义列表时,确实需要在方括号内填入具体的元素。我给出的答案中的错误解释是不正确的。

关于您的代码报错问题,经过仔细检查,原因是在循环语句中缺少循环条件,并且在变量`result`后没有给出具体的赋值表达式。

以下是修复后的代码:

  1. matrix = [[1, 2, 3],
  2.           [4, 5, 6],
  3.           [7, 8, 9],
  4.           [10, 11, 12]]

  5. rows = len(matrix)
  6. cols = len(matrix[0])
  7. top = 0
  8. right = cols - 1
  9. left = 0
  10. bottom = rows - 1
  11. result = []

  12. while left <= right and top <= bottom:
  13.     # 从左到右遍历上边
  14.     for i in range(left, right+1):
  15.         result.append(matrix[top][i])
  16.     top += 1

  17.     # 从上到下遍历右边
  18.     for i in range(top, bottom+1):
  19.         result.append(matrix[i][right])
  20.     right -= 1

  21.     if top <= bottom:
  22.         # 从右到左遍历下边
  23.         for i in range(right, left-1, -1):
  24.             result.append(matrix[bottom][i])
  25.         bottom -= 1

  26.     if left <= right:
  27.         # 从下到上遍历左边
  28.         for i in range(bottom, top-1, -1):
  29.             result.append(matrix[i][left])
  30.         left += 1

  31. print(result)
复制代码


这段代码会按照顺时针方向遍历矩阵,并将遍历结果存储在`result`列表中。希望可以解决您的问题!如有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-14 02:22:22 | 显示全部楼层
FishC 发表于 2024-4-14 02:18
这段代码报错是因为在定义列表`matrix`时,使用了两个逗号而没有填入具体的元素。在Python中,逗号用来分隔 ...

matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]],这是一个4*3的列表啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-14 02:22:34 | 显示全部楼层
captaindai 发表于 2024-04-14 02:22
matrix = [,,],这是一个4*3的列表啊


抱歉,我之前的回答有误导性。你在代码中使用 [,,] 创建了一个 4*3 的列表。实际上,这样的写法是不合法的,会导致语法错误。

要创建一个 4*3 的空列表,可以使用以下方法:
  1. matrix = [[], [], [], []]
复制代码


或者使用循环来创建一个空列表:
  1. matrix = []
  2. for i in range(4):
  3.     matrix.append([])
复制代码


这样就可以创建一个包含 4 行 3 列的空列表了。如果你想要给列表填充具体的值,可以在内层的方括号内添加元素,例如:
  1. matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
复制代码


希望这次能够解决你的问题,如果还有其他疑问,请随时提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-14 02:26:16 | 显示全部楼层
FishC 发表于 2024-4-14 02:22
抱歉,我之前的回答有误导性。你在代码中使用 [,,] 创建了一个 4*3 的列表。实际上,这样的写法是不合 ...
  1. matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
  2. rows = len(matrix)
  3. cols = len(matrix[0])
  4. top = 0
  5. right = cols - 1
  6. left = 0
  7. bottom = rows - 1
  8. result = []
  9. while left <= right and top <= bottom:
  10.     for col in range(left,right + 1):
  11.         result.append(matrix[top][col])
  12.     for row in range(top + 1,bottom + 1):
  13.         result.append(matrix[row][right])
  14.     if left < right and top < bottom:   
  15.         for col in range(right - 1,left,-1):
  16.             result.append(matrix[bottom][col])
  17.         for row in range(bottom - 1,top - 1):
  18.             result.append(matrix[row][left])
  19.     left += 1
  20.     top -= 1
  21.     bottom += 1
  22.     right -= 1

  23. print(result)
复制代码


  1. matrix = [[1, 2, 3, 4],
  2.           [5, 6, 7, 8],
  3.           [9, 10, 11, 12]]
  4.    
  5. rows = len(matrix)
  6. cols = len(matrix[0])
  7.    
  8. left = 0
  9. right = cols - 1
  10. top = 0
  11. bottom = rows - 1
  12.    
  13. result = []
  14.    
  15. while left <= right and top <= bottom:
  16.     # 从左往右遍历
  17.     for col in range(left, right + 1):
  18.         result.append(matrix[top][col])
  19.    
  20.     # 从上往下遍历
  21.     for row in range(top + 1, bottom + 1):
  22.         result.append(matrix[row][right])
  23.    
  24.     if left < right and top < bottom:
  25.         # 从右往左遍历
  26.         for col in range(right - 1, left, -1):
  27.             result.append(matrix[bottom][col])
  28.    
  29.         # 从下往上遍历
  30.         for row in range(bottom, top, -1):
  31.             result.append(matrix[row][left])
  32.    
  33.     left = left + 1
  34.     right = right - 1
  35.     top = top + 1
  36.     bottom = bottom - 1
  37.    
  38. print(result)
复制代码

这两段代码有什么区别
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-14 02:26:40 | 显示全部楼层
captaindai 发表于 2024-04-14 02:26
matrix = [,,]
rows = len(matrix)
cols = len(matr ...


非常抱歉,我之前的回答仍然有误。使用连续的逗号并不能正确创建一个4行3列的列表,这样的写法是错误的。

要创建一个具有4行3列的列表,可以使用如下的方式:

  1. matrix = [[0, 0, 0],
  2.           [0, 0, 0],
  3.           [0, 0, 0],
  4.           [0, 0, 0]]
复制代码


这样就创建了一个包含4行3列的列表,其中每个元素初始化为0。

接着,你可以使用`len(matrix)`来获取矩阵的行数和列数。在你的代码中,你想要获取矩阵的行数和列数,并将其分别赋值给`rows`和`cols`。但是由于`matrix`中没有元素,所以无法正确获取行数和列数。这会导致后面的代码出错。

请记得在创建列表时填入具体的元素,并确保在使用`len()`函数前已经为列表添加了元素,这样才能正确获取列表的长度。

如果你有其他问题,请随时提问!非常感谢你的理解。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-15 10:02:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2024-4-15 10:08 编辑
  1. while left <= right and top <= bottom:
  2. . . . . . .
  3.     left += 1          # left < right 小的增大,循环有终点 -【逻辑正确】
  4.     right -= 1         # left < right 大的减小,循环有终点 -【逻辑正确】
  5.     top -= 1           # top < bottom 小的减小,循环无终点 -【逻辑错误】
  6.     bottom += 1        # top < bottom 大的增大,循环无终点 -【逻辑错误】
复制代码

        【修改】:
  1.     top += 1           # top < bottom 小的增大,循环有终点 -【逻辑正确】
  2.     bottom -= 1        # top < bottom 大的减小,循环有终点 -【逻辑正确】
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 16:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表