鱼C论坛

 找回密码
 立即注册
查看: 2020|回复: 2

[已解决]求大佬帮助

[复制链接]
发表于 2020-11-11 01:51:02 | 显示全部楼层 |阅读模式

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

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

x
  1. def merge(lefthalf ,righthalf):
  2.     list1 = lefthalf + righthalf
  3.     list2 = []
  4.     print(list1)
  5.     for a in list1:
  6.         b = min(list1)
  7.         list2.append(b)
  8.         list1.remove(b)
  9.     print(list2)


  10. list1 = [1, 4, 6, 10]
  11. list2 = [5, 7, 8, 11, 12]
  12. print(merge(list1, list2))

复制代码




最后的运行结果为什么只会输出一部分 不会完整的将list1 排序出来  并且还会显示一个none?
最佳答案
2020-11-11 08:19:52
本帖最后由 Twilight6 于 2020-11-11 08:25 编辑

你 for 循环list1时候,每次循环 都会执行 list1.remove(b) ,导致列表中元素减少,所以循环会少几次,跳过几个元素

把 for 循环那改成 for a in list1[:]: 用列表切片拷贝一份就回移出list1元素时候不会受到影响,就可以正常的循环下去

而返回的是None 是因为你定义的函数没有设置 return 返回值,则Python默认返回一个None

不设置return ,直接调用函数,用函数内的print 函数进行打印参考代码:
  1. def merge(lefthalf ,righthalf):
  2.     list1 = lefthalf + righthalf
  3.     list2 = []
  4.     print(list1)
  5.     for a in list1[:]:
  6.         b = min(list1)
  7.         list2.append(b)
  8.         list1.remove(b)
  9.     print(list2)

  10. list1 = [1, 4, 6, 10]
  11. list2 = [5, 7, 8, 11, 12]
  12. merge(list1, list2)
复制代码


设置return 返回列表进行打印,参考代码:
  1. def merge(lefthalf ,righthalf):
  2.     list1 = lefthalf + righthalf
  3.     list2 = []
  4.     print(list1)
  5.     for a in list1[:]:
  6.         b = min(list1)
  7.         list2.append(b)
  8.         list1.remove(b)
  9.     return list2

  10. list1 = [1, 4, 6, 10]
  11. list2 = [5, 7, 8, 11, 12]
  12. print(merge(list1, list2))
复制代码

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

使用道具 举报

发表于 2020-11-11 08:19:52 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-11-11 08:25 编辑

你 for 循环list1时候,每次循环 都会执行 list1.remove(b) ,导致列表中元素减少,所以循环会少几次,跳过几个元素

把 for 循环那改成 for a in list1[:]: 用列表切片拷贝一份就回移出list1元素时候不会受到影响,就可以正常的循环下去

而返回的是None 是因为你定义的函数没有设置 return 返回值,则Python默认返回一个None

不设置return ,直接调用函数,用函数内的print 函数进行打印参考代码:
  1. def merge(lefthalf ,righthalf):
  2.     list1 = lefthalf + righthalf
  3.     list2 = []
  4.     print(list1)
  5.     for a in list1[:]:
  6.         b = min(list1)
  7.         list2.append(b)
  8.         list1.remove(b)
  9.     print(list2)

  10. list1 = [1, 4, 6, 10]
  11. list2 = [5, 7, 8, 11, 12]
  12. merge(list1, list2)
复制代码


设置return 返回列表进行打印,参考代码:
  1. def merge(lefthalf ,righthalf):
  2.     list1 = lefthalf + righthalf
  3.     list2 = []
  4.     print(list1)
  5.     for a in list1[:]:
  6.         b = min(list1)
  7.         list2.append(b)
  8.         list1.remove(b)
  9.     return list2

  10. list1 = [1, 4, 6, 10]
  11. list2 = [5, 7, 8, 11, 12]
  12. print(merge(list1, list2))
复制代码

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

使用道具 举报

 楼主| 发表于 2020-11-11 10:08:57 | 显示全部楼层
Twilight6 发表于 2020-11-11 08:19
你 for 循环list1时候,每次循环 都会执行 list1.remove(b) ,导致列表中元素减少,所以循环会少几次,跳过 ...
  1. def merge(lefthalf ,righthalf):
  2.     list1 = lefthalf + righthalf
  3.     list2 = []
  4.     print(list1)
  5.     a = len(list1)
  6.     b = len(list2)
  7.     while a >= b:
  8.         c = min(list1)
  9.         list2.append(c)
  10.         list1.remove(c)
  11.     print(list2)




  12. list1 = [1, 4, 6, 10]
  13. list2 = [5, 7, 8, 11, 12]
  14. print(merge(list1, list2))
复制代码



我把代码改成这样还是会报错  ,能不能帮我看看错在哪里了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 15:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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