9
$\begingroup$

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?

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

3 Answers 3

10
$\begingroup$

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

Length[listB] - Total[UnitStep[listB - # & /@ listA], {2}]
$\endgroup$
3
  • $\begingroup$ That's pretty, +1! $\endgroup$
    – ciao
    May 12, 2016 at 9:02
  • 1
    $\begingroup$ 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. $\endgroup$
    – ciao
    May 12, 2016 at 9:49
  • $\begingroup$ @ciao fixed, thanks. $\endgroup$
    – Kuba
    May 13, 2016 at 7:14
7
$\begingroup$
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}

$\endgroup$
6
  • $\begingroup$ Try these with large lists (say 50K elements each)... OP perhaps might specify if performance matters or if lists are beyond trivial sizes... $\endgroup$
    – ciao
    May 12, 2016 at 8:15
  • $\begingroup$ @ciao I did, and the Count and LengthWhile methods are drastically slower. I'll post the timing if you think it's useful $\endgroup$
    – Jason B.
    May 12, 2016 at 8:21
  • $\begingroup$ @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. $\endgroup$
    – justin
    May 12, 2016 at 8:26
  • $\begingroup$ 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 $\endgroup$
    – Jason B.
    May 12, 2016 at 8:30
  • $\begingroup$ I meant these will all be slow... $\endgroup$
    – ciao
    May 12, 2016 at 8:34
6
$\begingroup$

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...):

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.