将每个值[i]与单独的列表/数组中的另一个值[j]进行比较的最快方法是什么?

时间:2017-06-12 20:59:09

标签: python algorithm list loops

我有2个索引i和j的列表(实际上我有4个列表,但它们都是具有相同索引的时间和位置)。我需要将i中的每个单值与j中的每个单值进行比较,即i = 0 vs j = 0,1,2,3 ...然后i = 1 vs j = 0,1,2,3 ... I我正在比较i和j的时间和位置,所以我现在设置了两种不同的方式,但都没有我想要的那么快。我有一个非常简单的嵌套循环,如:

for i in range(0,len(a1)):
    for j in range(0,len(b1)):
        if ((a1[i] <= b1[j] + 100) & (a1[i] >= b1[j] - 100)):
            if angdist(a2.ra[i],a2.dec[i],b2.ra[j],b2.dec[j]) <= error[i]*u.degree: 
                print 'Found one that occurred at', a1[i]                    
                print 'It was located at', a2[i], 'with a likelihood value of', L

L是一个取决于i和j索引的值,但我认为在这里简化它会更容易。这个方法每个循环大约需要13秒,这非常慢。

我还有一个方法,它使用布尔数组来掩盖其中一个数组,因此我只需要进行&#34;行进&#34; 1个大阵列和1个微小阵列,以减少处理时间。

for i in range(0,len(a1)):
        bool_time = (b1 < a1[i] + 100) & (b1 > a1[i] - 100)
        angsep = angdist(a2.ra[i], a2.dec[i], b2[bool_time], b2.dec[bool_time])
        bool_ang = (angsep <= error[i]*u.degree)
        b_err = b3[bool_time][bool_ang]
        b_en = b3[bool_time][bool_ang]
        for x, y in zip(b_en,b_err):
            print 'Found one that occurred at', a1[i]                    
            print 'It was located at', a2[i], 'with a likelihood value of', L

这个方法每个循环大约需要2.5秒,但我需要使用乱码数据运行这些数千到数百万次,所以较短的时间,低于0.1秒是理想的。

由于我正在比较时间和位置,我考虑过一种方法,它只能比较a1 [i]与b1中的前几个j索引,然后在超出范围(100秒)后停止。我的问题是想出一种方法来启动和停止这个循环而不排除索引。停止部分很容易,但起点对我来说很困惑。

对于上下文,所有数组大约有400个索引,而b数组大约是14000个索引,但是当我获得更多数据时,这个数字将增加到超过10万个。

0 个答案:

没有答案