解压和 * 运算符 [英] Unzipping and the * operator

查看:44
本文介绍了解压和 * 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 文档将此代码作为 zip 的反向操作提供:

<预><代码>>>>x2, y2 = zip(*压缩)

特别是

<块引用>

zip() 与 * 运算符结合使用可用于解压缩列表.

有人可以向我解释 * 运算符在这种情况下是如何工作的吗?据我了解,* 是一个二元运算符,可用于乘法或浅拷贝......这里似乎都不是这种情况.

解决方案

当像这样使用时,*(星号,在某些圈子中也称为splat"运算符)是从列表中解压参数的信号.请参阅 http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists 以获得更完整的定义和示例.

The python docs gives this code as the reverse operation of zip:

>>> x2, y2 = zip(*zipped)

In particular

zip() in conjunction with the * operator can be used to unzip a list.

Can someone explain to me how the * operator works in this case? As far as I understand, * is a binary operator and can be used for multiplication or shallow copy...neither of which seems to be the case here.

解决方案

When used like this, the * (asterisk, also know in some circles as the "splat" operator) is a signal to unpack arguments from a list. See http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists for a more complete definition with examples.

这篇关于解压和 * 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆