Python Forum
Compare two lists. Count larger values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare two lists. Count larger values
#1
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
Reply
#2
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).
Reply
#3
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     
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 2,651 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Developing larger Neural Networks Chriskelm 2 2,789 Nov-03-2018, 02:47 AM
Last Post: brighteningeyes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020