python - why i cannot use chain expression in numpy?
问题描述
This don't work:
import numpy as np
np.random.random(10).sort()
this is ok:
Z = np.random.random(10)
Z.sort()
Please tell me why the chain expression don't work in Numpy.
解决方案
Just like list.sort
, numpy.ndarray.sort
sorts in-place, meaning that it mutates the array and then returns None
.
help(numpy.ndarray.sort)
->
sort(...)
a.sort(axis=-1, kind='quicksort', order=None)
Sort an array, in-place.
With
np.random.random(10).sort()
you won't see any output in the REPL because None
is not displayed.
(Even if this line produced a value other than None
, which it does not, it would be pretty pointless since you don't assign any name.)
In the second example, you create an array Z
and then sort it, which works as expected.
推荐阅读
- swift - 如何以编程方式检测 Swift 中的 Apple Pencil 模型?
- python - 在 Python 中打开 .txt 文件作为 Json 文件
- node.js - 400 Bad Request 仅适用于 Chrome 89.0.4389.116
- arrays - 在 Fortran 中快速交换数组中的二维
- c++ - 为什么我的 C++ 递归程序永远存在
- wordpress - 将属性从父级传递给子级 Gutenberg Blocks
- python - 测试停止后是否触发了工作节点中的事件挂钩?
- javascript - 为什么我的 jQuery 脚本不能用于两个 div?
- postgresql - 将 PostgreSQL 表流式传输到 Google BigQuery
- java - 我简单的spring boot hibernate映射问题