# Creating new list by comparing elements of two lists

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
Commented May 12, 2016 at 7:24
• @Danny - all your questions seem very similar: making listC from listA and listB Commented May 12, 2016 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
Commented May 12, 2016 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
Commented May 12, 2016 at 9:49
• @ciao fixed, thanks.
– Kuba
Commented May 13, 2016 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
Commented May 12, 2016 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 Commented May 12, 2016 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. Commented May 12, 2016 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 Commented May 12, 2016 at 8:30
• I meant these will all be slow...
– ciao
Commented May 12, 2016 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...):

Using Jason's data

a = {33, 651, 45, 947, 743, 964, 292, 182, 468, 563};
b = {739, 127, 687, 104, 840, 990, 475, 455, 4, 878};

Count[b, x_ /; x < #] & /@ a


{1, 5, 1, 9, 7, 9, 3, 3, 4, 5}

Using Jason's data:

a = {33, 651, 45, 947, 743, 964, 292, 182, 468, 563};
b = {739, 127, 687, 104, 840, 990, 475, 455, 4, 878};


Using Compile:

cf = Compile[{{a, _Integer, 1}, {b, _Integer, 1}},
Module[{result = ConstantArray[0, Length[a]]},
Do[result[[i]] = Total[UnitStep[a[[i]] - b]], {i, Length[a]}];
result], CompilationTarget -> "C", RuntimeOptions -> "Speed"]

cf[a, b] // RepeatedTiming

(*{6.57434*10^-6, {1, 5, 1, 9, 7, 9, 3, 3, 4, 5}}*)


An attempt was made at an answer like this, but it was wrong, deleted and I cannot comment to show the right way. So, I am adding it as an answer.

I am grabbing data from @Jason B.

a = {33, 651, 45, 947, 743, 964, 292, 182, 468, 563};
b = {739, 127, 687, 104, 840, 990, 475, 455, 4, 878};


and now

Count[False] /@ Outer[Less, a, b]


gives

{1, 5, 1, 9, 7, 9, 3, 3, 4, 5}

and of course, equivalently, we have

Count[True] /@ Outer[Greater, a, b]