我对python相当陌生,遇到了一个我无法解释的问题。我试着在这里搜索论坛的答案,但是我发现的与我的情况不符。感觉好像我错过了一些很基本的东西,但我没有看到它(显然…)
此代码按我预期的方式运行:import string
mults = [1,2,3,4,6,7,9,10,12,15,16,19,21,22,24]
def factor_exp(lst):
if lst[-1] == 1:
lst.pop()
return lst+[1]
if lst[-1] == 2:
lst.pop()
return lst+[1,1]
else:
return "Should never get here"
print factor_exp([1])
print factor_exp([2])
print factor_exp([1,2])
这将返回:
^{pr2}$
这就是我想要的。在
我认为在函数内部对列表使用append和extend也可以。在代码底部添加了一个“append”。在import string
mults = [1,2,3,4,6,7,9,10,12,15,16,19,21,22,24]
def factor_exp(lst):
if lst[-1] == 1:
lst.pop()
return lst+[1]
if lst[-1] == 2:
lst.pop()
return lst.append([1,1])
else:
return "Should never get here"
print factor_exp([1])
print factor_exp([2])
print factor_exp([1,2])
但这会带来:>>>
[1]
None
None
为什么会出现“无”呢?提前感谢您的任何帮助或见解。在