Creating new List by comparing 2 lists elements

Suppose I have two Lists (Not equal length). Both contain random numbers between 0 to 5000 (or another max limit):

A= {1000, 450, 50, 4100, ...,670}

B={500, 10, 4561, 2000, ...}


I would like to take each number from List A and compare it to all numbers in List B.

If the absolute value of B is smaller than A I will count it. If not I will not count it.

For example, in the Lists above, we start with 1000 in List A and compare it to all numbers in B, 500<1000, 10<1000... I will count all numbers that are smaller than 1000 and Build a new List C that contains integers which represents the amount of all numbers that were smaller than 1000 (here only 2), 450 (only 1), 50 (0), 4100 (3)...

In the next step I return the same procedure for 450 etc.

So C={2,1,0,3….}

Can someone help me and show me a way?

• Map[Count[list2, x_ /; x < #] &, list1] should get you started. If lists are huge, there are more efficient ways... – ciao May 12 '16 at 7:24
• @Danny - all your questions seem very similar: making listC from listA and listB – Jason B. May 12 '16 at 7:51

Between Jason's and ciao's in terms of performance:

Length[listB] - Total[UnitStep[listB - # & /@ listA], {2}]

• That's pretty, +1! – ciao May 12 '16 at 9:02
• Gotta hit the hay, late here - but you'll need to adjust it for lists not of the same length, it does not like them as written. – ciao May 12 '16 at 9:49
• @ciao fixed, thanks. – Kuba May 13 '16 at 7:14
listA = RandomInteger[1000, 10]
listB = RandomInteger[1000, 10]


Out[112]= {33, 651, 45, 947, 743, 964, 292, 182, 468, 563}

Out[113]= {739, 127, 687, 104, 840, 990, 475, 455, 4, 878}

First@First@Position[Sort@Join[{#}, listB], #] - 1 & /@ listA


Out[116]= {1, 5, 1, 9, 7, 9, 3, 3, 4, 5}

• Try these with large lists (say 50K elements each)... OP perhaps might specify if performance matters or if lists are beyond trivial sizes... – ciao May 12 '16 at 8:15
• @ciao I did, and the Count and LengthWhile methods are drastically slower. I'll post the timing if you think it's useful – Jason B. May 12 '16 at 8:21
• @JasonB:Could you tell how to run this code using Wolfram Mathematica Online.I get this " « Graphics ‘ Animation ‘ " is incomplete; more input is needed  while evaluating the code. – justin May 12 '16 at 8:26
• hmmm...... I would remove that first line, « Graphics‘Animation‘ , then I would replace the last line with ListAnimate[film] - never used MMA online, but it works in the desktop version – Jason B. May 12 '16 at 8:30
• I meant these will all be slow... – ciao May 12 '16 at 8:34

Here's a quck-and-dirty for lists of non-trivial size. There are some other ways perhaps faster, but more details on list composition would be useful before I spend further time.

With[{u = Union@#1},
Replace[#1,AssociationThread[u -> Ordering[Ordering[Join[u, #2]]][[;; Length@u]] -
Range@Length@u], 1]] &[list1, list2]


Results of a quick benchmark, taking successively longer slices of two 10K long lists (usual loungebook performance caveats...):