【问题标题】:Python star symbol in a loop [duplicate]循环中的Python星号[重复]
【发布时间】:2022-01-07 12:18:36
【问题描述】:

我正在使用 CPython,我在一个带有星号的示例文件中看到。 您能否解释一下* 符号在这种情况下的含义? 这里的pointsets 是来自pybind11numpy array,因为它是C++ 代码的输出。

Point(*point) 是否与 C++ 中的指针有关?

polylines = []
for points in pointsets:
    points = [Point(*point) for point in points]
    polyline = Polyline(points)
    polylines.append(polyline)

【问题讨论】:

标签: python cpython


【解决方案1】:

称为解包操作符。 以下是文档中的内容:

星号* 表示可迭代解包。它的操作数必须是 可迭代的。可迭代对象被扩展为一系列项目,这些项目是 包含在解包地点的新元组、列表或集合中。

它很像 Javascript ES6 中的“...”运算符。 (the spread operator)

https://docs.python.org/3/reference/expressions.html#expression-lists

【讨论】:

  • 谢谢你,这就是我正在寻找的。​​span>
  • @PetrasVestartasEPFL 很高兴为您提供帮助!考虑支持/接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 2020-10-18
  • 2016-10-04
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多