2

I have a list {a,b,c,d} and a second list where is how many times the elements of the first list repeat {1,4,2,6} (the order is according with the first list). Then I need to create a third list with this information: {a,b,b,b,b,c,c,d,d,d,d,d,d}. What can I do?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ask8
  • 637
  • 3
  • 6

1 Answers1

1
Catenate @ MapThread[ConstantArray, {{a, b, c, d}, {1, 4, 2, 6}}]

{a, b, b, b, b, c, c, d, d, d, d, d, d}

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Alan
  • 13,686
  • 19
  • 38
  • I'm leaving my answer but please vote for this instead (ht kglr). – Alan Mar 22 '18 at 02:29
  • 1
    Catenate not needed. Flatten@MapThread[ConstantArray, {{a, b, c, d}, {1, 4, 2, 6}}] works. Has advantage of working in older versions without Catenate. – m_goldberg Mar 22 '18 at 03:06