我试图找到这个 python numpy 代码的 rust 等效项,对数组进行元素比较。
import numpy as np
np.arange(3) > 1
这是我的 Rust 代码:
use ndarray::Array1;
fn main() {
let arr = Array1::<f64>::range(0.0, 3.0, 1.0);
arr > 1.0 // doesn't work.
}
我可以使用 for 循环来完成此操作,但我正在寻找最惯用的方法。
最佳答案
使用.map
对于逐元素操作:
fn main() {
let v: Vec<_> = (0..3).map(|x| x > 1).collect();
println!("{v:?}")
}
输出:
[false, false, true]
关于python - rust 中 ndarray::Array1<f64> 中的元素比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71469970/