表达式“dict(d1, **d2)”中的“**”是什么意思?

发布于 2024-08-21 13:08:23 字数 378 浏览 3 评论 0原文

我对下面的 python 表达式很感兴趣:

d3 = dict(d1, **d2)

任务是将两个字典合并到第三个字典中,上面的表达式很好地完成了任务。我对 ** 运算符以及它对表达式到底做了什么感兴趣。我认为 ** 是幂运算符,但尚未在上面的上下文中看到它的使用。

完整的代码片段是这样的:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3 = dict(d1, **d2)
>>> print d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

I am intrigued by the following python expression:

d3 = dict(d1, **d2)

The task is to merge 2 dictionaries into a third one, and the above expression accomplishes the task just fine. I am interested in the ** operator and what exactly is it doing to the expression. I thought that ** was the power operator and haven't seen it used in the context above yet.

The full snippet of code is this:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3 = dict(d1, **d2)
>>> print d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡紫姑娘! 2024-08-28 13:08:23

参数列表中的 ** 具有特殊含义,如 教程第 4.7 节。通过 **kwargs 传递的字典(或类字典)对象被扩展为可调用的关键字参数,就像 *args 被扩展为单独的位置参数一样。

** in argument lists has a special meaning, as covered in section 4.7 of the tutorial. The dictionary (or dictionary-like) object passed with **kwargs is expanded into keyword arguments to the callable, much like *args is expanded into separate positional arguments.

怼怹恏 2024-08-28 13:08:23

** 将字典变成关键字参数:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3 = dict(d1, **d2)

变成:

>>> d3 = dict(d1, c=3, d=4)

The ** turns the dictionary into keyword parameters:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3 = dict(d1, **d2)

Becomes:

>>> d3 = dict(d1, c=3, d=4)
〃温暖了心ぐ 2024-08-28 13:08:23

在Python中,任何函数都可以接受带有*的多个参数;
或多个带 ** 的关键字参数

接收端示例:

>>> def fn(**kwargs):
...   for kwarg in kwargs:
...     print kwarg
... 
>>> fn(a=1,b=2,c=3)
a
c
b

调用端示例(感谢 Thomas):

>>> mydict = dict(a=1,b=2,c=3)
>>> fn(**mydict)
a
c
b

In Python, any function can accept multiple arguments with *;
or multiple keyword arguments with **.

Receiving-side example:

>>> def fn(**kwargs):
...   for kwarg in kwargs:
...     print kwarg
... 
>>> fn(a=1,b=2,c=3)
a
c
b

Calling-side example (thanks Thomas):

>>> mydict = dict(a=1,b=2,c=3)
>>> fn(**mydict)
a
c
b
黄昏下泛黄的笔记 2024-08-28 13:08:23

还值得一提的是 dict 构造函数的机制。它采用初始字典作为其第一个参数,并且还可以采用关键字参数,每个关键字参数代表要添加到新创建的字典中的新成员。

It's also worth mentioning the mechanics of the dict constructor. It takes an initial dictionary as its first argument and can also take keyword arguments, each representing a new member to add to the newly created dictionary.

时常饿 2024-08-28 13:08:23

您已得到 ** 接线员的答复。这是添加字典的另一种方法

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3=d1.copy()
>>> d3.update(d2)
>>> d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

you have got your answer of the ** operator. here's another way to add dictionaries

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3=d1.copy()
>>> d3.update(d2)
>>> d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文