2

I have two lists:

list1={4,6}
list2={1,2}

and I now want a function f that produces

f[list1_,list2_]:={1,1,1,1,2,2,2,2,2,2}

so 4 times the 1 and 6 times the 2. f should be generic so that

3list1={2,3,7}; 3list2={1,2,3}

yields

res={1,1,2,2,2,3,3,3,3,3,3,3}

If you know several ways to do this please provide the fastest one. thanks a lot.

spore234
  • 601
  • 6
  • 10

2 Answers2

4
f[list1_, list2_] := Inner[Table[#2, {#1}] &, list1, list2, Join];
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
3
list1 = {4, 6}
list2 = {1, 2}

ClearAll[f];
f = Join @@ MapThread[Table, {#2, List /@ #1}] &

f[list1, list2]
(* {1, 1, 1, 1, 2, 2, 2, 2, 2, 2} *)

f[{2, 3, 7}, {1, 2, 3}]
(* {1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3} *)

or

h = Join @@ (Table @@@ Transpose[{#2, List /@ #1}]) &
h[{2, 3, 7}, {1, 2, 3}]
(* {1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3} *)

or

g = Join @@ (ConstantArray @@@ Transpose[{#2, #1}])&

g2 = Inner[ConstantArray, #2, #1, Join] &

Timings:

bh = Inner[Table[#2, {#1}] &, #1, #2, Join] &; (* BobHanlon *)

{listA, listB} = RandomInteger[100, {2, 100000}];
(rs1 = bh[listA, listB]); // Timing
(* {0.265625, Null} *)
(rs2 = g[listA, listB]); // Timing
(* {0.062500, Null} *)
(rs3 = g2[listA, listB]); // Timing
(* {0.078125, Null} *)
(rs4 = h[listA, listB]); // Timing
(* {0.296875, Null} *)
(rs5 = f[listA, listB]); // Timing
(* {0.296875, Null} *)
rs1 == rs2 == rs3 == rs4 == rs5
(* True *)
kglr
  • 394,356
  • 18
  • 477
  • 896
  • your solution is the fastest so far. – eldo Jun 03 '14 at 18:37
  • @eldo As usual I'm the slowest so far :) – mfvonh Jun 03 '14 at 18:46
  • 1
    With two 10^6 - Integer-Lists, MM 9.01 and 4 cores: Bob Hanlon -> 1.34, kguler -> 1.53, mfvonh -> 2.07, eldo -> 3.15 (up to 3 repetitions). – eldo Jun 03 '14 at 18:58
  • @kguler - why is "g" so much faster than "f" given the fact that both are kguler-offsprings :) – eldo Jun 03 '14 at 19:31
  • 1
    @eldo, it is because i could not get g working properly first time I tried and f worked right away :) I cannot locate the source on this site right now, but Join@@.. is usually faster than Flatten and ConstantArray is also faster than alternatives like Table and Array. – kglr Jun 03 '14 at 19:50