0

有没有最快的方法来执行以下 python 代码:

     L1 = [1,1,1,1,0,0]     
     L2 = [0,0,1,1,0,1]  

     D1 = [0.03,0.04,0.01,0.02,0.01,0.02]    
     D2 = [0.05,0.01,0.03,0.07,0.12,0.41]    
     a = 0
     b = 0        

     for l1,l2,d1,d2 in zip(L1,L2,D1,D2):

            if l1 == 1:      
                a += d1        
            if l2 == 1:    
                b += d2    

L1、L2、D1 和 D2 的长度相同,大约有 4000 个条目。

谢谢!

4

2 回答 2

2

如果速度真的很重要并且你有大量这样的数字列表,你可以使用 numpy,这在这里会很有帮助:

L1 = np.array([1,1,1,1,0,0])
L2 = np.array([0,0,1,1,0,1])

D1 = np.array([0.03,0.04,0.01,0.02,0.01,0.02])
D2 = np.array([0.05,0.01,0.03,0.07,0.12,0.41])

最快的方法是使用向量点积:

a = np.dot(D, L)

其他方式包括,其中的值的总和,D1其中的值L1评估为True

a = D1[L1.astype(bool)].sum()  
b = D2[L2.astype(bool)].sum()

或取乘积之D1L1

a = np.sum(D1*L1)
b = np.sum(D2*L2)

对于时间:

L = np.random.random_integers(0,1,4000)
D = np.random.rand(4000)

In [60]: timeit np.dot(D,L)
100000 loops, best of 3: 13.1 µs per loop

In [61]: timeit np.sum(D*L)
10000 loops, best of 3: 33.7 µs per loop

In [62]: timeit D[L.astype(bool)].sum()
10000 loops, best of 3: 65 µs per loop

In [67]: %%timeit
   ....: a = 0
   ....: for l, d in zip(L, D):
   ....:     if l == 1:
   ....:         a += d
   ....: 
100 loops, best of 3: 7.68 ms per loop
于 2013-10-09T17:16:39.910 回答
1

使用itertools.compress

import itertools
a = sum( itertools.compress(D1, L1) )
b = sum( itertools.compress(D2, L2) )

compress返回一个迭代器,该迭代器由第一个参数的那些元素组成,其中第二个参数的对应元素是True.

于 2013-10-09T17:15:28.177 回答