Python:最简单的方法来压缩包含函数中另一个tupple的tupple

cal*_*doa 2 python tuples unpack

我的代码是这样的:

def f1():
    return 2, 3

def f2():
    return 1, f1()
Run Code Online (Sandbox Code Playgroud)

我可以:

a, (b, c) = f2()
Run Code Online (Sandbox Code Playgroud)

我想要做:

a, b, c = f2()
Run Code Online (Sandbox Code Playgroud)

我能找到的所有解决方案都需要使用大量疯狂的括号/括号,或创建一个使用*运算符的标识函数.我想只修改f2().

有什么比这更简单的了吗?

zon*_*ndo 8

而不是使用1, f2(),使用元组串联:

def f2():
    return (1,) + f1()
Run Code Online (Sandbox Code Playgroud)

如评论中所述,您也可以这样做:

def f2():
    x,y = f1()
    return 1, x, y
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

def f2():
    return (lambda *args: args)(1, *f1())
Run Code Online (Sandbox Code Playgroud)

这有点长,但它比x,y = f1()解决方案有优势,因为这种方式f1()可以返回包含任意数量元素的元组.