小编NoN*_*ble的帖子

在 Python 2 中将多个列表和字典解包为函数参数

可能的相关问题(我搜索了相同的问题,但没有找到)


https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists 中所述,Python 提供了一种使用星号将参数解包为函数的便捷方法

>>> list(range(3, 6))            # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))            # call with arguments unpacked from a list
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我正在调用这样的函数:

def func(*args):
    for arg in args:
        print(arg)
Run Code Online (Sandbox Code Playgroud)

在 Python 3 中,我这样称呼它:

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]

func(*a, *b, *c)
Run Code Online (Sandbox Code Playgroud)

哪些输出

1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)

然而,在 Python 2 …

python python-2.x python-3.x

5
推荐指数
1
解决办法
824
查看次数

标签 统计

python ×1

python-2.x ×1

python-3.x ×1