Hi all
I have two lists A and B
and I want to compare them element-wise, to check how more often (as percentage) list A has a larger value than list B.
In R that would have been a for loop, but I think in python are smarter ways to work with lists?
Thanks a lot
Alex
In general case, when A[i] and B[i] are arbitrary numbers, I don't think that it is possible to solve the problem without loops. However, for example, if you know some additional information about the relation between A[i] and B[i], you can do it without loops: e.g. if B[i] = A[i]**2 and all A[i] are non-zero integers, you can definitely conclude that all B[i] >= A[i] (and ratio is 1). Probably, you are asking about how to make loops work faster. You can do this, if you switch to numpy, e.g.
import numpy as np
A_np = np.array(A)
B_np = np.array(B)
percentage = (A_np > B_np).sum() / len(A_np)
Another approach is to use jit-compilation, e.g. numba jit-compiler.
Note, when you are using numpy, you are still relying on a loop, but this loop is executed on more lower level (as a compiled C-procedure/module).
Just for clarification: in Python there are lists; in numpy there are arrays.
Item-wise comparison is pretty simple with lists - use zip(). However, how should be equals counted?:
>>> spam = [1, 5, 3, 6]
>>> ham = [2, 3, 4, 7]
>>> [second < first for first, second in zip(spam, ham)]
[False, True, False, False] # booleans are subclass of int
>>> sum(second < first for first, second in zip(spam, ham)) / len(spam)
0.25