[英]Increasing the speed of finding an index of an item in list
I'm trying to speed up a program I've written, and after importing cProfile, I see that one function takes up a massive bit of computation time. 我正在尝试加速我编写的程序,在导入cProfile之后,我发现一个函数占用了大量的计算时间。
It's this, which finds an numpy.ndarray in a list: 就是这样,它在列表中找到了一个numpy.ndarray:
def locate(arr, l ):
for i in range(len(l)):
if np.all(l[i] == arr):
return i
return -1
As the list can't be ordered etc, I can't see any way to avoid scanning the entire list. 由于列表无法订购等,我看不到任何方法可以避免扫描整个列表。 I have read some pieces on vectorisation, and I wanted to know if that could be applied here, or if there's any other way to speed this up? 我已经阅读了一些关于矢量化的文章,我想知道是否可以在这里应用,或者是否还有其他方法来加快速度?
Thanks 谢谢
You probably cannot avoid walking the list but you can speed up the comparison: 你可能无法避免走在列表中,但你可以加快比较:
Set up example: 设置示例:
L = list(np.floor(np.outer(*2*(np.linspace(1,10,1000),))))
arr = L[537]
Direct method for reference: 直接参考方法:
import itertools as it
next(it.chain((i for i, a in enumerate(L) if np.all(arr==a)), (-1,)))
# 537
timeit(lambda: next(it.chain((i for i, a in enumerate(L) if np.all(arr==a)), (-1,))), number=100)
# 0.27100146701559424
Approach 1: Use np.array_equal
(slower) 方法1:使用np.array_equal
(较慢)
next(it.chain((i for i, a in enumerate(L) if np.array_equal(arr, a)), (-1,)))
# 537
timeit(lambda: next(it.chain((i for i, a in enumerate(L) if np.array_equal(arr, a)), (-1,))), number=100)
# 0.2992244770284742
Approach 2: Use void view (faster) 方法2:使用void视图(更快)
arr_v = arr.reshape(-1).view(f'V{arr.itemsize*arr.size}')
next(it.chain((i for i, a in enumerate(L) if arr_v==a.reshape(-1).view(f'V{a.itemsize*a.size}')), (-1,)))
# 537
timeit(lambda: next(it.chain((i for i, a in enumerate(L) if arr_v==a.reshape(-1).view(f'V{a.itemsize*a.size}')), (-1,))), number=100)
# 0.11853155982680619
有一个名为index()
的内置python函数,您可以通过将字符串作为值插入并在列表中查找其索引来使用它。
So are you looking for np.where
所以你在寻找np.where
temp_list=np.array(temp_list)
np.where(temp_list==5)
(array([1, 3, 6, 8]),)
How to Check if a Matrix is in a List of Matrices Python 如何检查矩阵是否在Python矩阵列表中
Here the accepted answer uses np.array_equal
which first checks shape, then does the all(==)
test. 这里接受的答案使用np.array_equal
,它首先检查形状,然后进行all(==)
测试。
Another SO: Check if 2d array exists in 3d array in Python? 另一个SO: 检查Python中的3d数组中是否存在2d数组?
Searching an array for a value faster than np.where(ar==value) using fortran and f2py 使用fortran和f2py在数组中搜索比np.where(ar == value)更快的值
Why isn't "numpy.any" lazy (short-circuiting) 为什么不是“numpy.any”懒惰(短路)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.