比较列表中的每个元素与另一个列表中的相应元素的最快方法是什么?

时间:2018-10-08 06:41:38

标签: python python-3.x list comparison list-comprehension

我想将一个列表中的每个元素与另一个列表中的相应元素进行比较,以查看它是否更大或更小。

list1 = [4,1,3]
list2 = [2,5,2]

因此,将421532进行比较。

除了使用for循环以外,还有其他快速方法吗?

7 个答案:

答案 0 :(得分:4)

您可以为此使用numpy库。而且速度更快

>>> import numpy as np
>>> list1 = np.array([4,1,3]) 
>>> list2 = np.array([2,5,2])
>>> list1 < list2
array([False,  True, False])

运行该功能所花费的时间

>>> import timeit
>>> timeit.timeit("""
... import numpy as np
... list1 = np.array([4,1,3])
... list2 = np.array([2,5,2])
... print(list1 < list2)
... """,number=1)
[False  True False]
0.00011205673217773438

如果您研究numpy的实现,那么numpy基本上是用C,C ++编写的。

答案 1 :(得分:2)

您可以use zip() to连接列表元素和列表理解以创建结果列表:

 list1-value greater then list2-value: [True, False, True]

输出:

for (var i = 0; i < $scope.exhibitions.length; i++){
    if (i > 0){
        alert($scope.exhibitions[i].NAME);
    }
}

这与普通循环的工作原理相同-但看起来更像Python。

答案 2 :(得分:2)

您可以这样做

使用lambda:

In [91]: map(lambda x,y:x<y,list1,list2)
Out[91]: [False, True, False]

使用zip和for循环:

In [83]: [i<j for i,j in zip(list1,list2)]
Out[83]: [False, True, False]

lambda和for循环的执行时间:

In [101]: def test_lambda():
     ...:     map(lambda x,y:x>y,list1,list2)
     ...:     

In [102]: def test_forloop():
     ...:     [i<j for i,j in zip(list1,list2)]
     ...:     

In [103]: %timeit test_lambda
     ...: 
10000000 loops, best of 3: 21.9 ns per loop

In [104]: %timeit test_forloop
10000000 loops, best of 3: 21 ns per loop

答案 3 :(得分:2)

您可以将两个列表映射到一个运算符,例如int.__lt__(“小于”运算符):

list(map(int.__lt__, list1, list2))

使用示例输入,将返回:

[False, True, False]

答案 4 :(得分:1)

为了更好地进行比较,如果所有方法都以相同的方式计时:

paul_numpy:  0.15166378399999303
artner_LCzip:  0.9575707100000272
bhlsing_map__int__:  1.3945185019999826
rahul_maplambda:  1.4970900099999653
rahul_LCzip:  0.9604789950000168

用于计时的代码:

setup_str = '''import numpy as np
list1 = list(map(int, np.random.randint(0, 100, 1000000)))
list2 = list(map(int, np.random.randint(0, 100, 1000000)))'''


paul_numpy = 'list1 = np.array(list1); list2 = np.array(list2);list1 < list2'
t = timeit.Timer(paul_numpy, setup_str)
print('paul_numpy: ', min(t.repeat(number=10)))

artner = '[a > b for a,b in zip(list1,list2)]'
t = timeit.Timer(artner, setup_str)
print('artner_LCzip: ', min(t.repeat(number=10)))

blhsing = 'list(map(int.__lt__, list1, list2))'
t = timeit.Timer(blhsing, setup_str)
print('bhlsing_map__int__: ', min(t.repeat(number=10)))

rahul_lambda = 'list(map(lambda x,y:x<y,list1,list2))'
t = timeit.Timer(rahul_lambda, setup_str)
print('rahul_maplambda: ', min(t.repeat(number=10)))

rahul_zipfor = '[i<j for i,j in zip(list1,list2)]'
t = timeit.Timer(rahul_zipfor, setup_str)
print('rahul_LCzip: ', min(t.repeat(number=10)))

答案 5 :(得分:0)

实际上,我应该说没有一种比循环更快速的方法来执行所需的操作,因为如果您更接近问题,可以发现通过使用算法,该操作至少需要O(n)完成。因此,所有答案都是正确的,并且可以使用Zipmap或...来实现,但是这些解决方案只会使您的实现更加美观和pythonic。速度不是很快,在某些情况下,例如急躁的回答,由于代码行少而不是时间复杂,所以速度要快一点。

答案 6 :(得分:0)

使用枚举无需在此处使用任何zipmap

[item > list2[idx] for idx, item in enumerate(list1)]
# [True, False, True]