以相反顺序访问 numpy 数组的 Pythonic 方法

标签 python numpy

我正在尝试重写一些看起来像是由 FORTRAN 程序员编写的代码,以使其更具 Python 风格/可读性。下面是感兴趣的代码片段。代码的总体行为是,只要 Z 的值小于 1,就将 Z 的前三个元素存储到 Zstart 中,并且只要 Z 的后三个值小于 1,也将它们存储到 Zend 中。

import numpy as np
nbpoints = 3
Z = np.linspace(0,1.0,10)
Zstart = np.ones(nbpoints)
Zend = np.ones(nbpoints)
Zpts = np.size(Z)
for j in range(nbpoints):
    if Z[j] < Zstart[j]:
       Zstart[j] = Z[j]
    if Z[Zpts - 1 - j] < Zend[nbpoints - 1 - j]:
        Zend[nbpoints - 1 - j] = Z[Zpts - 1 - j]

从两端反向移动 Zstart 和 Zend 的访问让我有点困惑。我当前的解决方案如下。

import numpy as np
nbpoints = 3
Z = np.linspace(0,1.0,10)
Zstart = np.ones(nbpoints)
Zend = np.ones(nbpoints)
Zpts = np.size(Z)
for j in range(nbpoints):
    if Z[j] < Zstart[j]:
       Zstart[j] = Z[j]
    if Z[-(j+1)] < Zend[-(j+1)]:
        Zend[-(j+1)] = Z[-(j+1)]

此代码的示例输出是:

Z = [ 0.0 0.11111111  0.22222222  0.33333333  0.44444444  0.55555556 
0.66666667  0.77777778  0.88888889  1.0 ]

Zstart = [ 0.0 0.11111111  0.22222222]
Zend = [ 0.77777778  0.88888889  1.0 ]

我的解决方案感觉就像我仍然只是重写写得不好的代码,例如重新安排泰坦尼克号甲板上的椅子。有没有更Pythonic的方式来执行这个操作?

最佳答案

您不需要使用 np.ones 实例化 ZstartZend。只需直接构建它们即可:

nbpoints = 3
Z = np.linspace(0,1.0,10)
Zstart = Z[:nbpoints][Z[:nbpoints] < 1]
Zend = Z[-nbpoints:][Z[-nbpoints:] < 1]

print(Zstart)
print(Zend)
# [ 0.          0.11111111  0.22222222]    
# [ 0.77777778  0.88888889]

请注意,Zend 这里只有 2 个元素,因为 Z 中的最后一个元素不小于 1。

关于以相反顺序访问 numpy 数组的 Pythonic 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46713489/

相关文章:

python - 如何将 Numpy 数组转换为 Panda DataFrame

python - Numpy:从两个向量(或一个向量本身)的笛卡尔积创建一个矩阵,同时将一个函数应用于所有对

python - 使用 pcolor 叠加两个图

python - 用于 Jenkins/本地构建的 PyPI 本地缓存

python - Pandas dataframe applymap并行执行

python - 在 python 中延迟抽取随机结果

python - 连接大文件,管道,还有一个好处

python - 从数据框中的文本列中选择唯一的组合

python - 如何使用 Python 访问属于对象的动态 C++ 数组变量?

python - 尝试用 selenium/beautiful soup 提取动态表(url 不变)