我目前正在学习 Rust,作为第一个练习,我想实现一个计算第 n 个斐波那契数的函数:
fn main() {
for i in 0..48 {
println!("{}: {}", i, fibonacci(i));
}
}
fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
Run Code Online (Sandbox Code Playgroud)
我将其运行为:
fn main() {
for i in 0..48 {
println!("{}: {}", i, fibonacci(i));
}
}
fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + …Run Code Online (Sandbox Code Playgroud) 我有一个数据框:
df = pl.DataFrame(
{
"t_left": [0.0, 1.0, 2.0, 3.0],
"t_right": [1.0, 2.0, 3.0, 4.0],
"counts": [1, 2, 3, 4],
}
)
Run Code Online (Sandbox Code Playgroud)
我想将每一行映射到一个列表中,然后收集所有值(要传递到 例如matplotlib.hist)
我可以手动完成,如下所示:
times = []
for t_left, t_right, counts in df.rows():
times.extend(np.linspace(t_left, t_right, counts + 1)[1:])
Run Code Online (Sandbox Code Playgroud)
但这对于我的数据集来说太慢了。
我对 python 和 Polars 都是全新的,所以我想知道是否有更好的方法来实现这一点。
编辑
完整的复制粘贴示例以重现。
import polars as pl
import numpy as np
size = 1000000
df = pl.DataFrame(
{
"t_left": np.random.rand(size),
"t_right": np.random.rand(size) + 1,
"counts": [1] * size,
}
)
times = []
for …Run Code Online (Sandbox Code Playgroud)