Unpacking variable length list returned from function

后端 未结 5 600
鱼传尺愫
鱼传尺愫 2021-01-25 18:44

Ok so I\'m defining a function that takes a variable number of inputs and clamps each of them

def clamp(*args):
    return [ max(min(arg, 0.8), 0.2) for arg in a         


        
5条回答
  •  暖寄归人
    2021-01-25 19:17

    I think when you call it with one element, you can add a , after the name. a, = clamp(1). In this case, you don't have to modify your function and the automatic unpacking still happens.

    >>> a, b, c = [1,2,3]
    >>> a
    1
    >>> b
    2
    >>> c
    3
    >>> a, = [1]
    >>> a
    1
    >>> 
    

提交回复
热议问题