Python 将一个列表的每个元素与另一个列表的相应元素进行比较的最快方法是什么?
我想将一个列表中的每个元素与另一个列表中相应的元素进行比较,看看它是大还是小Python 将一个列表的每个元素与另一个列表的相应元素进行比较的最快方法是什么?,python,python-3.x,list,comparison,list-comprehension,Python,Python 3.x,List,Comparison,List Comprehension,我想将一个列表中的每个元素与另一个列表中相应的元素进行比较,看看它是大还是小 list1 = [4,1,3] list2 = [2,5,2] 因此,比较4与2,1与5,3与2 除了使用for循环,还有其他快速方法吗?您可以使用numpy库来实现此目的。而且速度要快得多 >>> import numpy as np >>> list1 = np.array([4,1,3]) >>> list2 = np.array([2,5,2]) >
list1 = [4,1,3]
list2 = [2,5,2]
因此,比较4
与2
,1
与5
,3
与2
除了使用for循环,还有其他快速方法吗?您可以使用
numpy
库来实现此目的。而且速度要快得多
>>> import numpy as np
>>> list1 = np.array([4,1,3])
>>> list2 = np.array([2,5,2])
>>> list1 < list2
array([False, True, False])
>>将numpy作为np导入
>>>list1=np.array([4,1,3])
>>>list2=np.array([2,5,2])
>>>列表1<列表2
数组([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
导入timeit
>>>timeit.timeit(“”)
…将numpy作为np导入
…list1=np.array([4,1,3])
…list2=np.array([2,5,2])
…打印(列表1<列表2)
…”,数字=1)
[假-真-假]
0.00011205673217773438
你可以使用<代码> NoMPy < /Co>库。而且速度要快得多
>>> import numpy as np
>>> list1 = np.array([4,1,3])
>>> list2 = np.array([2,5,2])
>>> list1 < list2
array([False, True, False])
>>将numpy作为np导入
>>>list1=np.array([4,1,3])
>>>list2=np.array([2,5,2])
>>>列表1<列表2
数组([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
导入timeit
>>>timeit.timeit(“”)
…将numpy作为np导入
…list1=np.array([4,1,3])
…list2=np.array([2,5,2])
…打印(列表1<列表2)
…”,数字=1)
[假-真-假]
0.00011205673217773438
你可以加入列表元素和列表理解来创建结果列表:
list1 = [4,1,3]
list2 = [2,5,2]
list1_greater_list2_value = [a > b for a,b in zip(list1,list2)]
print ("list1-value greater then list2-value:", list1_greater_list2_value)
输出:
list1-value greater then list2-value: [True, False, True]
这与普通循环的工作原理相同,但看起来更像Python。您可以加入列表元素和列表理解来创建结果列表:
list1 = [4,1,3]
list2 = [2,5,2]
list1_greater_list2_value = [a > b for a,b in zip(list1,list2)]
print ("list1-value greater then list2-value:", list1_greater_list2_value)
输出:
list1-value greater then list2-value: [True, False, True]
这与普通循环的工作原理相同,但看起来更像蟒蛇。您可以这样做
lambda的使用:
In [91]: map(lambda x,y:x<y,list1,list2)
Out[91]: [False, True, False]
lambda和for循环的执行时间:
In [83]: [i<j for i,j in zip(list1,list2)]
Out[83]: [False, True, False]
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
[101]中的:def test_lambda():
…:映射(λx,y:x>y,列表1,列表2)
...:
在[102]:def test_forloop()中:
你可以这样做
lambda的使用:
In [91]: map(lambda x,y:x<y,list1,list2)
Out[91]: [False, True, False]
lambda和for循环的执行时间:
In [83]: [i<j for i,j in zip(list1,list2)]
Out[83]: [False, True, False]
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
[101]中的:def test_lambda():
…:映射(λx,y:x>y,列表1,列表2)
...:
在[102]:def test_forloop()中:
…:[i您可以将这两个列表映射到一个操作符,例如int.\uu lt\uu
(“小于”操作符):
对于示例输入,这将返回:
[False, True, False]
您可以将这两个列表映射到一个操作符,例如int.\uu lt\uu
(“小于”操作符):
对于示例输入,这将返回:
[False, True, False]
实际上,我应该说,除了循环之外,没有更快的方法来完成你想要的动作,因为如果你更仔细地观察问题,你会发现,通过使用算法,这个动作至少需要O(n)
待完成。因此,所有答案都是正确的,并且可以使用Zip
或map
或…按照您的意愿执行,但这些解决方案只会让您的实现更漂亮、更通俗。不是更快,在某些情况下,例如blhsing answer,它会更快一点,因为代码行更少,而不是时间复杂性。事实上,我应该这样做假设没有比循环更快的方法来完成你想要的动作,因为如果你仔细观察问题,你会发现通过使用算法,这个动作至少需要O(n)
待完成。因此,所有答案都是正确的,并且可以使用Zip
或map
或…按照您的意愿执行,但这些解决方案只会使您的实现更漂亮、更通俗。不是更快,在某些情况下,例如blhsing answer,它会更快一点,因为代码行更少,而不是时间复杂性。只是为了为了更好地进行比较,如果所有进近的时间安排相同:
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)))
setup\u str=''将numpy作为np导入
list1=list(map(int,np.random.randint(011001000)))
list2=list(map(int,np.random.randint(011001000))“”
paul_numpy='list1=np.array(list1);list2=np.array(list2);list1b代表a,b在zip中(列表1,列表2)]”
t=timeit.Timer(artner,setup_str)
打印('artner_LCzip:',最小值(t.repeat(number=10)))
blhsing='列表(映射(内部,列表1,列表2))'
t=timeit.Timer(blhsing,setup\u str)
打印('bhlsing_map__int__:',最小值(t.repeat(number=10)))
rahul_lambda='列表(图(lambda x,y:x仅为了更好的比较,如果所有方法都以相同的方式计时:
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)))
setup\u str=''将numpy作为np导入
list1=list(map(int,np.random.randint(011001000)))
list2=list(map(int,np.random.randint(011001000))“”
paul_numpy='list1=np.array(list1);list2=np.array(list2);list1b代表a,b在zip中(列表1,列表2)]”
t=timeit.Timer(artner,setup_str)
打印('artner_LCzip:',最小值(t.repeat(number=10)))
blhsing='列表(映射(内部,列表1,列表2))'
t=timeit.Timer(blhsing,setup\u str)
打印('bhlsing_map__int__:',最小值(t.repeat(number=10)))
rahul_lambda='列表(使用enumerate时,lambda x,y:x无需在此处使用任何zip
或map
[item > list2[idx] for idx, item in enumerate(list1)]
# [True, False, True]
使用enumerate无需在此处使用任何zip
或map
[item > list2[idx] for idx, item in enumerate(list1)]
# [True, False, True]
OP:除了使用for循环,还有其他快速方法吗?
@mehrdad pedramfar更新。使用lambda和zip比for循环快得多吗?@Brock更新了计时。OP:除了使用for循环,还有其他快速方法吗?
@mehrdad pedramfar更新。使用lambda和zip比for循环快得多吗?@B摇动