首页 > 解决方案 > 将 Python 字典列表传递给 Rust 函数 PyO3

问题描述

我正在尝试在 rust 中编写一个函数,我可以从 python 调用该函数,该函数接受一个字典列表(想想 pandas 类似数据框的数据)并从 rust 访问这些键和值。我怎样才能做到这一点?我正在使用pyo3. 我是否需要定义一个与 python dict 输入的键、值对匹配的结构?

作为一个示例函数,我试图传入一个字典列表并将与键对应的值相加key为一个总数。我的 python 字典列表中的每个字典都有key对应于 int 的键。

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use pyo3::types::PyDict;

#[pyfunction]
fn sum_list_dicts(a: Vec<PyDict>, key: String) -> PyResult<i32> {
    let mut tot = 0_i32;

    for d in a.iter() {
        tot += d[key];
    }
    Ok(tot)
}

#[pymodule]
fn rustpy(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_list_dicts, m)?)?;

    Ok(())
}

标签: pythondictionaryrustpyo3

解决方案


所以这真的取决于你真正想要做什么。如果你不想弄乱实际的Py项目,你可以简单地这样做:

#[pyfunction]
fn sum_list_dicts(a: Vec<HashMap<String, i32>>, key: String) -> PyResult<i32> {
    let mut tot = 0_i32;

    for d in a.iter() {
        tot += d[&key];
    }
    Ok(tot)
}

如果您想使用PyListPyDict,这也可以:

#[pyfunction]
fn sum_list_dicts(a: &PyList, key: String) -> PyResult<i32> {
   let mut tot = 0_i32;

   for d in a.iter() {
       tot += d.downcast::<PyDict>()?.get_item(&key).unwrap().downcast::<PyInt>()?.extract::<i32>()?;
   }
   Ok(tot)
}

使用任何一种方法,您都可以简单地从 Python 端调用它:

a = [{"ham":0, "eggs":0}, {"eggs": 1}, {"eggs": 3, "spam":2}]
b = sum_list_dicts(a, "eggs")
print(b)
>>> 4

推荐阅读