python - rust 中 ndarray::Array1<f64> 中的元素比较

标签 python multidimensional-array rust comparison elementwise-operations

我试图找到这个 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]

Playground

关于python - rust 中 ndarray::Array1<f64> 中的元素比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71469970/

相关文章:

python - 正则表达式匹配函数参数

python - 使用另一个数据帧替换数据帧中的空值

java - 如何计算 Java 中的二维 int 数组?

rust - 计数选项集合中出现次数的惯用方式

generics - 在Rust中编写一个将可迭代容器作为参数的泛型函数

python - 通过集成创建一个新的 lambda 函数

python - 调用 groupby 函数后如何将您的数据帧设为 "autofill"?

Javascript 在多维数组中添加数据

python - 2D 或 3D 中的 Numpy trim_zeros

rust - 错误[E0599] : no method named `gen` found for type `&mut G` in the current scope