python - 将 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(())
}
解决方案
所以这真的取决于你真正想要做什么。如果你不想弄乱实际的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)
}
如果您想使用PyList
等PyDict
,这也可以:
#[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
推荐阅读
- vue.js - 如何在方法函数中使用计算函数?
- cloud - CloudStack 无法添加主机
- swift - 如何获得最顶层的视图并使用它在 SwiftUI 中呈现特定视图?
- sql - SQL 在 2 个日期之间获取名称,但也保留任何以前的日期
- ios - 如何在 SwiftUI 中创建从登录视图到选项卡视图的过渡?
- javascript - 在 React Hooks useState() 中更改作为参数传递的类的数据
- java - Player*First*JoinEvent 我应该怎么做?
- vue.js - 在容器中放置 v-app-bar 内容有问题吗?
- sql - SQL 字节截断
- javascript - 如何使箭头键始终能够移动我的角色?