2024-05-17 01:13:03 发布
网友
如果一个函数返回两个值,如何将第二个值直接从函数结果附加到列表中? 像这样:
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
试试all_stuff += zip(get_stuff())[1]
all_stuff += zip(get_stuff())[1]
可以使用与列表[]相同的索引对tuple进行索引。因此,如果需要第二个元素list,可以从函数调用的返回中索引元素[1]。你知道吗
[]
tuple
list
[1]
def get_stuff(): return 'a string', [1,2,3,5] all_stuff = [6,7] all_stuff.extend(get_stuff()[1])
输出
[6, 7, 1, 2, 3, 5]
试试
all_stuff += zip(get_stuff())[1]
可以使用与列表
[]
相同的索引对tuple
进行索引。因此,如果需要第二个元素list
,可以从函数调用的返回中索引元素[1]
。你知道吗输出
相关问题 更多 >
编程相关推荐