简体   繁体   English

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

[英]Unpacking multiple lists and dictionaries as function arguments in Python 2

Possible Relevant Questions (I searched for the same question and couldn't find it) 可能的相关问题(我搜索了相同的问题,但没有找到)


Python makes a convenient way to unpack arguments into functions using an asterisk, as explained in https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-listshttps://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]

In my code, I'm calling a function like this:在我的代码中,我正在调用这样的函数:

def func(*args):
    for arg in args:
        print(arg)

In Python 3, I call it like this:在 Python 3 中,我这样称呼它:

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

func(*a, *b, *c)

Which outputs哪些输出

1 2 3 4 5 6 7 8 9

In Python 2, however, I encounter an exception:然而,在 Python 2 中,我遇到了一个异常:

>>> func(*a, *b, *c)
  File "<stdin>", line 1
    func(*a, *b, *c)
             ^
SyntaxError: invalid syntax
>>> 

It seems like Python 2 can't handle unpacking multiple lists.似乎 Python 2 无法处理多个列表的解包。 Is there a better and cleaner way to do this than有没有比这更好、更干净的方法来做到这一点

func(a[0], a[1], a[2], b[0], b[1], b[2], ...)

My first thought was I could concatenate the lists into one list and unpack it, but I was wondering if there was a better solution (or something that I don't understand).我的第一个想法是我可以将列表连接成一个列表并解压缩它,但我想知道是否有更好的解决方案(或我不明白的东西)。

d = a + b + c
func(*d)

Recommendation: Migrate to Python 3建议:迁移到 Python 3
As of January 1, 2020, the 2.x branch of the Python programming language is no longer supported by the Python Software Foundation.自 2020 年 1 月 1 日起,Python 软件基金会不再支持 Python 编程语言的 2.x 分支。

Unpacking Lists and passing to *args解包列表并传递给*args

Python 3 Solution Python 3 解决方案

def func(*args):
    for arg in args:
        print(arg)

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

func(*a, *b, *c)

Python 2 Solution Python 2 解决方案

If Python 2 is required, then itertools.chain will provide a workaround:如果需要 Python 2,则itertools.chain将提供解决方法:

import itertools


def func(*args):
    for arg in args:
        print(arg)

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

func(*itertools.chain(a, b, c))

Output输出

1
2
3
4
5
6
7
8
9

Unpacking Dictionaries and passing to **kwargs解压字典并传递给**kwargs

Python 3 Solution Python 3 解决方案

def func(**args):
    for k, v in args.items():
        print(f"key: {k}, value: {v}")


a = {"1": "one", "2": "two", "3": "three"}
b = {"4": "four", "5": "five", "6": "six"}
c = {"7": "seven", "8": "eight", "9": "nine"}

func(**a, **b, **c)

Python 2 Solution Python 2 解决方案

As Elliot mentioned in the comments, if you need to unpack multiple dictionaries and pass to kwargs , you can use the below:正如Elliot在评论中提到的,如果您需要解压多个字典并传递给kwargs ,您可以使用以下内容:

import itertools

def func(**args):
    for k, v in args.items():
        print("key: {0}, value: {1}".format(k, v))


a = {"1": "one", "2": "two", "3": "three"}
b = {"4": "four", "5": "five", "6": "six"}
c = {"7": "seven", "8": "eight", "9": "nine"}

func(**dict(itertools.chain(a.items(), b.items(), c.items())))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM