我想在 Fortran 中做类似的事情:
program where
real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)
print *, a(a>7)
end program
import numpy
a = numpy.array([ 4, 5, 6, 7, 8, 9])
print a[numpy.where(a>7)]
#or
print a[a>7]
我玩过,但到目前为止没有任何效果,但我猜它相当简单。
最佳答案
我将稍微扩展@VladimirF 的答案,因为我怀疑您不想将自己限制在确切的打印示例中。
a>7
返回与 a
对应的 logical
数组,其中 .true.
位于条件的索引处满足,.false.
否则。 pack
内部函数采用这样的掩码并返回一个数组,其中包含掩码中带有 .true.
的那些元素。
但是,您可以使用可能适合您 numpy.where
愿望的掩码做其他事情。例如,有 where
构造(和 where
语句)和 merge
内在函数。此外,您可以再次使用 pack
和掩码来获取索引并进行更多相关操作。
关于numpy - Fortran 相当于 numpy.where() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21435589/