python - python 中的冒号放在数组后面有什么作用?

标签 python numpy

知道在这种情况下冒号应该做什么吗? 我只是偶然发现了这一点,发现它不会产生任何错误,我很好奇它是否做了什么。到目前为止,我没有看到冒号在这种情况下做任何事情。

有人可以启发我吗?

import numpy as np
x = np.arange(1, 10)
x
output: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

x:5
x
output: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

enter image description here

最佳答案

正如评论所说,您可能正在寻找 slice notation但实际上这不再是一个SyntaxError。您为变量 x 提供了 5 的类型注释(这实际上没有任何意义)。

您可以查看模块的 __annotations__ 来查看它是否已添加到此处,例如

print(__annotations__)
x : 5
print(__annotations__)

将打印

{}
{'x': 5}

有关变量注释的更多详细信息,请参阅 PEP 526 .

关于python - python 中的冒号放在数组后面有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58121177/

相关文章:

python - 使用查询在 Python 中使用 Flask 获取大数据并显示在 HTML 表中

python - 如何获取 numpy 数组中重复元素的所有索引的列表

python - 删除Qlabel中不必要的空白

python - 如果列全部为 nan,则使 numpy.nanargmin 返回 nan

python - 有效地计算唯一元素的数量 - NumPy/Python

python - 有没有办法将 3D 高斯分布或高斯混合分布拟合到向量?

python - Scipy - 如何进一步优化随机梯度下降的稀疏矩阵代码

python - python中的while循环,输入和字符串有问题

python - pygame - 更新得分框

python - 按属性对对象列表进行排序不排序?