将元素添加到 *args 列表并进一步传递结果
Adding elements to *args list and passing the result further
我想在我的代码中使用以下结构:
def target_function(*args, key=value, key2=value2, key3=value3, key4=value4):
print(*args)
def function_prepending_arguments(*args, key4=value4):
target_function(["a", "b", "c"] + *args, key4=key4) # does not work, *args is a tuple
对于 function_prepending_arguments(["c", "d", "e"], key4="dummy")
我希望在我的输出中看到 ['a', 'b', 'c', 'd', 'e']
。
我如何实现这一点(如果可能的话,以最 pythonic 的方式)?
问题是您不能连接列表和元组。因此,只需传递 ('a', 'b', 'c') + args
。或者['a', 'b', 'c'] + list(args)
对我有用的是
def function_prepending_arguments(*args, key=value):
target_function(['a', 'b', 'c'] + list(*args), key=key)
感谢blue_note将我推向正确的方向!
更好的方法是
x = [1,2,3]
# y = tuple/generator/list
y = (4,5,6)
x.extend(y) # won't return anything.
我想在我的代码中使用以下结构:
def target_function(*args, key=value, key2=value2, key3=value3, key4=value4):
print(*args)
def function_prepending_arguments(*args, key4=value4):
target_function(["a", "b", "c"] + *args, key4=key4) # does not work, *args is a tuple
对于 function_prepending_arguments(["c", "d", "e"], key4="dummy")
我希望在我的输出中看到 ['a', 'b', 'c', 'd', 'e']
。
我如何实现这一点(如果可能的话,以最 pythonic 的方式)?
问题是您不能连接列表和元组。因此,只需传递 ('a', 'b', 'c') + args
。或者['a', 'b', 'c'] + list(args)
对我有用的是
def function_prepending_arguments(*args, key=value):
target_function(['a', 'b', 'c'] + list(*args), key=key)
感谢blue_note将我推向正确的方向!
更好的方法是
x = [1,2,3]
# y = tuple/generator/list
y = (4,5,6)
x.extend(y) # won't return anything.