是否有更好的方法将参数附加到Python列表中?

问题描述 投票:-2回答:1

编写一个名为append_three_elements的函数。该函数将四个参数作为参数。首先是我们要附加的列表,接下来的三个和要添加到列表中的值。此函数应返回一个新列表,并在末尾按顺序附加三个值。

例如,append_three_elements([],1,2,3)

您希望返回[1、2、3]

def append_three_elements(a,b,c, lst):
    new_lst = lst
    for i in append_three_elements(a,b,c, lst = []):
        new_lst.append(i)
    return new_lst
python list append
1个回答
0
投票

这是一个无止境的递归函数。它会一直绕转而不会停止。请尝试以下方法:

def append_three_elements(a, b, c, lst):
    new_lst = lst.copy()
    for i in (a, b, c):
        new_lst.append(i)
    return new_lst
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.