如何在 Python 中将第二个返回值直接附加到列表中

Dio*_*ves 1 python

如果函数返回两个值,如何将函数结果中的第二个值直接附加到列表中?像这样的东西:

def get_stuff():
    return 'a string', [1,2,3,5]

all_stuff = [6,7]
# How do I add directly from the next line, without the extra code?
_, lst = get_stuff()
all_stuff += lst
Run Code Online (Sandbox Code Playgroud)

Cor*_*mer 5

tuple您可以使用与列表相同的索引来索引 a []。因此,如果您想要list第二个元素,您可以[1]从函数调用的返回结果中索引该元素。

def get_stuff():
    return 'a string', [1,2,3,5]

all_stuff = [6,7]
all_stuff.extend(get_stuff()[1])
Run Code Online (Sandbox Code Playgroud)

输出

[6, 7, 1, 2, 3, 5]
Run Code Online (Sandbox Code Playgroud)