鱼C论坛

 找回密码
 立即注册
查看: 1186|回复: 6

[已解决]023,024讲课后作业01递归解决append insert出来列表顺序一样?

[复制链接]
发表于 2020-6-13 04:09:54 | 显示全部楼层 |阅读模式

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

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

x
023,024讲课后作业01递归解决append insert出来列表顺序一样?
写一个函数get_digits(n),将参数n分解出每个位的数字并按顺序存放到列表中。举例:get_digits(12345) ==> [1, 2, 3, 4, 5]

写的递归版本
  1. >>> def fun1(n):
  2.         lenght=len(str(n))
  3.         def fun2(lenght):
  4.                 str1=''
  5.                 list1=[]
  6.                 if lenght==0:
  7.                         return list1
  8.                 else:
  9.                         str1=str(n//10**(lenght-1))
  10.                         list1.insert(0,(str1[-1]))
  11.                         return fun2(lenght-1)+list1
  12.         return fun2(lenght)

  13. >>> fun1(123456)
  14. ['6', '5', '4', '3', '2', '1']
复制代码

这是加到列表第一个位置

  1. >>> def fun1(n):
  2.         lenght=len(str(n))
  3.         def fun2(lenght):
  4.                 str1=''
  5.                 list1=[]
  6.                 if lenght==0:
  7.                         return list1
  8.                 else:
  9.                         str1=str(n//10**(lenght-1))
  10.                         list1.append(str1[-1])
  11.                         return fun2(lenght-1)+list1
  12.         return fun2(lenght)

  13. >>> fun1(123456)
  14. ['6', '5', '4', '3', '2', '1']
复制代码


这是append,加到列表最后一个位置,出来结果一样。
这是为什么,有什么办法在函数内把列表翻转过来输出?

  1. def fun1(n):
  2.         lenght=len(str(n))
  3.         temp_str1=''
  4.         list1=[]
  5.         for i in range(1,lenght+1):
  6.                 temp_str1=str(n//(10**(i-1)))
  7.                 list1.insert(0,(temp_str1[-1]))

  8.         return list1
复制代码


这是迭代的办法用insert就可以正常输出
最佳答案
2020-6-13 07:34:22



把你代码的else 代码块下的 return fun2(lenght-1)+list1 位置对调下即可

但是注意 小甲鱼要返回的是 列表里面是整数的,而你把他变成了字符串,在你代码的 str1[-1] 那转为 int 就可
  1. def fun1(n):
  2.     lenght = len(str(n))

  3.     def fun2(lenght):
  4.         list1 = []
  5.         if lenght == 0:
  6.             return list1
  7.         else:
  8.             str1 = str(n // 10 ** (lenght - 1))
  9.             list1.insert(0,int((str1[-1])))
  10.             return list1+fun2(lenght - 1)

  11.     return fun2(lenght)
  12. print(fun1(123456))
复制代码


但是其实不用内嵌函数,直接一个函数就够了:
  1. list1 = []
  2. def fun1(n):
  3.     if n > 0:
  4.         list1.insert(0,n%10)
  5.         return fun1(n//10)
  6. fun1(123456)
  7. print(list1)
复制代码

直接在原列表上进行添加元素



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

使用道具 举报

发表于 2020-6-13 05:17:28 From FishC Mobile | 显示全部楼层
没看明白你想问什么啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 07:34:22 | 显示全部楼层    本楼为最佳答案   



把你代码的else 代码块下的 return fun2(lenght-1)+list1 位置对调下即可

但是注意 小甲鱼要返回的是 列表里面是整数的,而你把他变成了字符串,在你代码的 str1[-1] 那转为 int 就可
  1. def fun1(n):
  2.     lenght = len(str(n))

  3.     def fun2(lenght):
  4.         list1 = []
  5.         if lenght == 0:
  6.             return list1
  7.         else:
  8.             str1 = str(n // 10 ** (lenght - 1))
  9.             list1.insert(0,int((str1[-1])))
  10.             return list1+fun2(lenght - 1)

  11.     return fun2(lenght)
  12. print(fun1(123456))
复制代码


但是其实不用内嵌函数,直接一个函数就够了:
  1. list1 = []
  2. def fun1(n):
  3.     if n > 0:
  4.         list1.insert(0,n%10)
  5.         return fun1(n//10)
  6. fun1(123456)
  7. print(list1)
复制代码

直接在原列表上进行添加元素



评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
小甲鱼的铁粉 + 1 + 1 感谢楼主无私奉献!

查看全部评分

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

使用道具 举报

发表于 2020-6-13 08:35:21 | 显示全部楼层
  1. def get_digits(num, /):
  2.     result = []
  3.     while num:
  4.         result.append(num % 10)
  5.         num //= 10
  6.     result.reverse()
  7.     return result
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 17:41:55 | 显示全部楼层
Twilight6 发表于 2020-6-13 07:34
把你代码的else 代码块下的 return fun2(lenght-1)+list1 位置对调下即可

但是注意 小甲鱼要返回 ...

懂了。。换个位置,递归函数返回的时候就倒着顺序加了吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 17:42:49 | 显示全部楼层
wp231957 发表于 2020-6-13 05:17
没看明白你想问什么啊

额,我可能描述不清楚。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 17:45:29 | 显示全部楼层

这个迭代的代码好简洁,就是num后面'/‘没看懂什么意思。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 17:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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