hat*_*rix 19 python arrays numpy multidimensional-array
NumPy有三元运算符吗?例如,在R中有一个矢量化if-else函数:
> ifelse(1:10 < 3,"a","b")
[1] "a" "a" "b" "b" "b" "b" "b" "b" "b" "b"
Run Code Online (Sandbox Code Playgroud)
NumPy中有什么相同的东西吗?
Eri*_*got 26
您正在寻找numpy.where():
>>> print numpy.where(numpy.arange(10) < 3, 'a', 'b')
['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b']
Run Code Online (Sandbox Code Playgroud)
NumPy的甚至有一个概括(即映射为0,1,2,等,以值,而不是仅映射真和假): numpy.choose().