How to compare two lists in python

Suppose I have two lists (or numpy.array s):

 a = [1,2,3] b = [4,5,6] 

How to check if each element a smaller than the corresponding element b in the same index? (I assume the indices start at 0) i.e.

 at index 0 value of a = 1 < value of b = 4 at index 1 value of a = 2 < value of b = 5 at index 2 value of a = 3 < value of b = 6 

If a were equal [1,2,7] , this would be wrong, because the value of index 2 a greater than the value of b . In addition, if the length of a was less than that of b , it should only compare indices a with indices b .

For example, this pair a , b

 a = [1,2] b = [3,4,5] 

at indices 0 and 1, the value of a less than b , so this will also check.

PS--> I have to use the above conditions inside the if . And also no element from a should be equal to b , i.e. Strictly less. Feel free to use as many tools as you want. (Although I use lists here, you can convert the above lists to numpy arrays too.)

+6
source share
4 answers

As an option, quick and short

 from operator import lt from itertools import starmap, izip all(starmap(lt, izip(a, b))) 
+4
source

Answer both parts with zip and all

 all(i < j for (i, j) in zip(a, b)) 

zip associates the values ​​from the beginning of a with the values ​​from the beginning of b ; the iteration ends when the shorter iterative has ended. all returns True if and only if all elements in the given state are true in a boolean context. In addition, when any item fails, False will be returned earlier.

Examples of results:

 >>> a = [1,2,3] >>> b = [4,5,6] >>> all(i < j for (i, j) in zip(a, b)) True >>> a = [1,2,7] >>> b = [4,5,6] >>> all(i < j for (i, j) in zip(a, b)) False >>> a = [1,2] >>> b = [4,5,-10] >>> all(i < j for (i, j) in zip(a, b)) True 

Timing with IPython 3.4.2:

 In [1]: a = [1] * 10000 In [2]: b = [1] * 10000 In [3]: %timeit all(i < j for (i, j) in zip(a, b)) 1000 loops, best of 3: 995 Β΅s per loop In [4]: %timeit all(starmap(lt, zip(a, b))) 1000 loops, best of 3: 487 Β΅s per loop 

Thus, in this case, starmap is faster. In general, in Python 2, things are relatively slow: function calls and name queries. starmap Retard solution seems to win here precisely because the tuple obtained from zip can be served as something like * args to the built-in lt , while my code should deconstruct it.

+12
source

Since this question has a numpy tag, I decided that I provided a numpy solution.

You can use the < operator on arrays equal length, so if your arrays has different lengths, you need to shorten the longest.

 In [26]: import numpy as np In [27]: a = [1,2,3] In [28]: b = [4,5,6] In [29]: np.all(a < b) Out[29]: True In [30]: a = [1,2] In [31]: b = [3,4,5] In [32]: shortest = min(len(a), len(b)) In [33]: np.all(a[:shortest] < b[:shortest]) Out[33]: True 
+2
source

Just an alternative way and not sure how it works against other answers:

 a = [1,2,3] b = [4,5,6] if filter(lambda x: x[0] < x[1], zip(a,b)): return True 

I use it in the if statement, since the question indicates that it will be used in this way, and there is no need for a bool conversion. Otherwise, I would wrap the filter () inside bool (). Again, this is just a demonstration of an alternative approach, and not the point of being most effective.

+1
source

Source: https://habr.com/ru/post/983107/


All Articles