使用Python递归完全可以分开
def extractlyEvenlyDivisibleHelp(inputlst, number, outputlst = []):
if number == 0:
print([])
return
if inputlst[0] % number == 0:
outputlst.append(inputlst[0])
outputlst.sort()
inputlst.remove(inputlst[0])
if len(inputlst) == 0:
print(outputlst)
else:
extractlyEvenlyDivisibleHelp(inputlst, number, outputlst)
def extractlyEvenlyDivisible(input, number):
extractlyEvenlyDivisibleHelp(input, number, [])
extractlyEvenlyDivisible([1,2,3,4,5,6,7,8,9], 0)
extractlyEvenlyDivisible([1,2,-3,4,5,-6,7,8,9,9,6], -3)
extractlyEvenlyDivisible([1,2,3,4,5,6,7,8,9,10, 10], 5)
输出:
[ ]
[-6, -3, 6, 9, 9]
[5, 10, 10]
预期输出:
[ ]
[-6, -3, 6, 9]
[5, 10]
我需要帮助,因为我只需要一个9,我的意思是,如果我输入几次相同的位数,它应该只返回一次。
def extractlyEvenlyDivisibleHelp(inputlst, number, outputlst = []):
if number == 0:
print([])
return
if inputlst[0] % number == 0:
outputlst.append(inputlst[0])
outputlst.sort()
inputlst.remove(inputlst[0])
if len(inputlst) == 0:
print(outputlst)
else:
extractlyEvenlyDivisibleHelp(inputlst, number, outputlst)
def extractlyEvenlyDivisible(input, number):
extractlyEvenlyDivisibleHelp(input, number, [])
extractlyEvenlyDivisible([1,2,3,4,5,6,7,8,9], 0)
extractlyEvenlyDivisible([1,2,-3,4,5,-6,7,8,9,9,6], -3)
extractlyEvenlyDivisible([1,2,3,4,5,6,7,8,9,10, 10], 5)
Output:
[ ]
[-6, -3, 6, 9, 9]
[5, 10, 10]
Expected output:
[ ]
[-6, -3, 6, 9]
[5, 10]
I need help, as I need only one 9, I mean if I input couple times the same digit, it should return only one time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在使用 noreferrer”> <<<<代码> set :
也不好的做法是重新定义
input
的内置,即使在函数中,我也会将其更改为其他变量名称You could filter out duplicates from the input list before passing to the helper using
set
:Also it is bad practice to redefine built-ins like
input
even in a function, I would change this to a different variable name