鱼C论坛

 找回密码
 立即注册
查看: 1179|回复: 5

[已解决]请问python新版课程中的第25讲课后作业答案最后一个为什么要有着一条语句:

[复制链接]
发表于 2022-1-8 09:31:23 | 显示全部楼层 |阅读模式
20鱼币
  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)
复制代码


我试过了,这里去掉第 24 行的代码也能正常运行
所以说,第 24 行代码究竟有什么作用?!!!
哪位大佬可以解释一下的?
解释出来的人,重金悬赏20鱼币!!!
最佳答案
2022-1-8 09:31:24
  1. matrix = [[1],
  2.           [5],
  3.           [9]]

  4. rows = len(matrix)
  5. cols = len(matrix[0])

  6. left = 0
  7. right = cols - 1
  8. top = 0
  9. bottom = rows - 1

  10. result = []

  11. while left <= right and top <= bottom:
  12.     # 从左往右遍历
  13.     for col in range(left, right + 1):  # 不跑
  14.         result.append(matrix[top][col])

  15.     # 从上往下遍历
  16.     for row in range(top + 1, bottom + 1):  # 这个跑完结果为 [1, 5, 9]
  17.         result.append(matrix[row][right])

  18.     # 从右往左遍历
  19.     for col in range(right - 1, left, -1):  # 不跑
  20.         result.append(matrix[bottom][col])

  21.     # 从下往上遍历
  22.     for row in range(bottom, top, -1):  # 这个会多跑一遍 1, 5, 9, 9, 5
  23.         result.append(matrix[row][left])

  24.     left = left + 1
  25.     right = right - 1
  26.     top = top + 1
  27.     bottom = bottom - 1

  28. print(result)
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-8 09:31:24 | 显示全部楼层    本楼为最佳答案   
  1. matrix = [[1],
  2.           [5],
  3.           [9]]

  4. rows = len(matrix)
  5. cols = len(matrix[0])

  6. left = 0
  7. right = cols - 1
  8. top = 0
  9. bottom = rows - 1

  10. result = []

  11. while left <= right and top <= bottom:
  12.     # 从左往右遍历
  13.     for col in range(left, right + 1):  # 不跑
  14.         result.append(matrix[top][col])

  15.     # 从上往下遍历
  16.     for row in range(top + 1, bottom + 1):  # 这个跑完结果为 [1, 5, 9]
  17.         result.append(matrix[row][right])

  18.     # 从右往左遍历
  19.     for col in range(right - 1, left, -1):  # 不跑
  20.         result.append(matrix[bottom][col])

  21.     # 从下往上遍历
  22.     for row in range(bottom, top, -1):  # 这个会多跑一遍 1, 5, 9, 9, 5
  23.         result.append(matrix[row][left])

  24.     left = left + 1
  25.     right = right - 1
  26.     top = top + 1
  27.     bottom = bottom - 1

  28. print(result)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-1-8 09:38:07 | 显示全部楼层
兄弟们,加油,一起来思考!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-8 09:47:10 | 显示全部楼层
本帖最后由 大马强 于 2022-1-8 09:48 编辑

跑是能跑,但是你看他的结果已经变了,
后面跟着的两个for循环恰好落在前边的for循环中,这才没有报错,
如果你把那两个循环提外边的话。在没有if的束缚下
  1. matrix = [[1],
  2.           [5],
  3.           [9]]
复制代码

这个矩阵的结果为 [1, 5, 9, 9, 5]

而源代码结果为 [1, 5, 9],所以区别在这
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-1-8 10:28:16 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-1-8 10:29:53 | 显示全部楼层

eee懂了,牛呀!大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 15:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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