删除嵌套for循环找到重合值 [英] Removing nested for loop to find coincidence values

查看:158
本文介绍了删除嵌套for循环找到重合值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用嵌套for循环遍历数组来查找符合特定条件的值。问题是这种方法效率低下,耗时。有人告诉我,更好的方法是根据数据对两个数组进行排序,但是这需要我将几个1D数组和一个多维数组按照一列进行排序,然后再将它们分开。有没有更有效的方法来做到这一点?下面是我的代码示例:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $' []
plane1Times = np.array([[2293902],[2848853],[482957]])
plane2Times = np.array([[7416504],[2613113],[2326542]])
plane1Local = np.array([[0,0,0],[0,u,0],[0,2 * u,0],[u,0,0],[u,u, 0],[U,2 * U,0],[2 * U,0,0],[2 * U,U,0],[2 * U,2 * U,0],[3 * U, 0,0],[3 * u,u,0],[3 * u,2 * u,0]],dtype ='float')
plane2Local = np.array([[0,0, d],[0,U,d],[0,2 * U,d],[U,0,d],[U,U,d],[U,2 * U,d],[2 * U,0,d],[2 * U,U,d],[2 * U,2 * U,d],[3 * U,0,d],[3 * U,U,d],[ (0,len(plane1Times)):
tic = time.time()
(0,len(plane2Times)):
if plane2Times [n] - plane1Times [i]< = 10000 and plane2Times [n] - plane1Times [i]> 0:
x1 = plane1Local [plane1Dets [i]]
x2 = plane2Local [plane2DetScale [n]]
distance = np.sqrt((x2 [0] -x1 [0])* * 2 +(x2 [1] -x1 [1])** 2 +(x2 [2])** 2)
timeSeparation =(plane2Times [n] -plane1Times [i])* timeScale
速度+ =距离/时间分隔
break

给你一个例子目前正在采取,每个时间阵列的长度是10 ** 6个值,所以在 i 中的100个循环大约需要60秒。有人可以帮我吗?

解决方案

我不能真正测试,因为你提供的代码是不完整的,但这是一个可能解决方案

 索引,enumerate(plane1Times)中的值:
vec = plane2Times - value
row, ((vec <10000)&(vec> 0))
如果len(row)> 0:
x1 = plane1Local [plane1Dets [index]]
x2 = plane2Local [plane2DetScale [row [0]]]
distance = np.sqrt((x2 [0] - x1 [0 ])** 2 +(x2 [1] - x1 [1])** 2 +(x2 [2])** 2)
timeSeparation =(plane2Times [row [0]] - plane1Times [index] )* timeScale
velocity + = distance / timeSeparation

消除第二个循环,一次减法。然后搜索新的数组,在那里它符合您的标准。由于看起来你想要第一个值,只需要像 row [0] 这样的第一个索引来获得值检查的索引。删除第二个循环应该大大减少时间。

I am currently using a nested for loop to iterate through to arrays to find values that match a certain criterion. The problem is that this method is incredibly inefficient and time consuming. I was told that a better way might be to sort the two arrays based on the data, but this requires me to combine several 1D arrays and one multi-D array, sort based on one column, then separate them again. Is there a more efficient way of doing this? Here is a sample of my code:

x1 = []
x2 = []
velocity = []
plane1Times = np.array([[2293902],[2848853],[482957]])
plane2Times = np.array([[7416504],[2613113],[2326542]])
plane1Local = np.array([[0,0,0],[0,u,0],[0,2*u,0],[u,0,0],[u,u,0],[u,2*u,0],[2*u,0,0],[2*u,u,0],[2*u,2*u,0],[3*u,0,0],[3*u,u,0],[3*u,2*u,0]],dtype='float')
plane2Local = np.array([[0,0,D],[0,u,D],[0,2*u,D],[u,0,D],[u,u,D],[u,2*u,D],[2*u,0,D],[2*u,u,D],[2*u,2*u,D],[3*u,0,D],[3*u,u,D],[3*u,2*u,D]],dtype='float')
for i in range(0,len(plane1Times)):
    tic = time.time()
    for n in range(0,len(plane2Times)):
        if plane2Times[n] - plane1Times[i] <= 10000 and plane2Times[n] - plane1Times[i] > 0:
            x1 = plane1Local[plane1Dets[i]]
            x2 = plane2Local[plane2DetScale[n]]
            distance = np.sqrt((x2[0]-x1[0])**2 + (x2[1]-x1[1])**2 + (x2[2])**2)
            timeSeparation = (plane2Times[n]-plane1Times[i])*timeScale
            velocity += distance/timeSeparation
            break

To give you an example of the time it is currently taking, each array of times is 10**6 values long so 100 loops in i takes about 60 seconds. Can someone please help me?

解决方案

I cant really test because the code you provided isn't complete, but this is a possible solution

for index,value in enumerate(plane1Times):
    vec = plane2Times - value
    row,col = np.where((vec<=10000)&(vec>0))
    if len(row) > 0:
        x1 = plane1Local[plane1Dets[index]]
        x2 = plane2Local[plane2DetScale[row[0]]]
        distance = np.sqrt((x2[0] - x1[0]) ** 2 + (x2[1] - x1[1]) ** 2 + (x2[2]) ** 2)
        timeSeparation = (plane2Times[row[0]] - plane1Times[index]) * timeScale
        velocity += distance / timeSeparation

Eliminate the second loop, and just do the subtraction all at once. Then search the new array, where it meats your criteria. Since it seems that you want the first value, just take the first index like row[0] to get the index of the value check. Removing the second for loop should drop the time considerably.

这篇关于删除嵌套for循环找到重合值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆