Skip to main content
 首页 » 编程设计

python之python 是否有像容器一样的 dict 但像 numpy.array 一样快

2025年05月04日6rubylouvre

我知道 numpy.array 比 python 内置列表快得多,占用的内存也少得多。 有没有类似 dict 但快得多的东西?我只需要存储 int : int 或 int : float 数据。

请您参考如下方法:

我会看一下来自 pandas 的 series .从示例中可以看出,它与 np.arrays 配合使用效果很好:

import numpy as np 
 
from pandas import * 
 
randn = np.random.randn 
 
In [309]: s = Series(randn(5), index=randn(5)) 
 
In [310]: s 
Out[310]:  
 1.968290    0.132438 
-0.307750    0.158168 
 0.288507    2.129288 
 1.002813   -0.247056 
-0.450041    1.731273 
 
In [311]: foo = np.array([0., 1.5, 1.]) 
 
In [312]: s = Series(foo) 
 
In [313]: s 
Out[313]:  
0    0.0 
1    1.5 
2    1.0