Repeat elements in list, but the number of times each element is repeated is provided by a separate listComparing elements of the $n^textth$ sublist in a ragged list with the $n^textth$ member of a sequenceMaking a list of rules from the list of elementsHow to gather a list whitout no repeat element to nearest a certain numberChanging the order of elements in a listSubtract second element of element of list from other list if the first elements are equalNumber elements in a listSelect first element in deepest list of each nested listHow to repeat each element in a list and the whole list as well?

Why NASA publish all the results/data it gets?

Wired to Wireless Doorbell

Gas leaking in base of new gas range?

As an employer, can I compel my employees to vote?

Where are they calling from?

Resolving moral conflict

Nanomachines exist that enable Axolotl-levels of regeneration - So how can crippling injuries exist as well?

Is it true that, "just ten trading days represent 63 per cent of the returns of the past 50 years"?

Where Does VDD+0.3V Input Limit Come From on IC chips?

What can a pilot do if an air traffic controller is incapacitated?

The 100 soldier problem

CDG baggage claim before or after immigration?

How to deal with my team leader who keeps calling me about project updates even though I am on leave for personal reasons?

How would a native speaker correct themselves when they misspeak?

Do liquid propellant rocket engines experience thrust oscillation?

Can planetary bodies have a second axis of rotation?

How to make interviewee comfortable interviewing in lounge chairs

Escape the labyrinth!

Manager encourages me to take day of sick leave instead of PTO, what's in it for him?

Leaving a job that I just took based on false promise of a raise. What do I tell future interviewers?

GitHub repo with Apache License version 2 in package.json, but no full license copy nor comment headers

I reverse the source code, you negate the output!

How does one calculate the distribution of the Matt Colville way of rolling stats?

How do rulers get rich from war?



Repeat elements in list, but the number of times each element is repeated is provided by a separate list


Comparing elements of the $n^textth$ sublist in a ragged list with the $n^textth$ member of a sequenceMaking a list of rules from the list of elementsHow to gather a list whitout no repeat element to nearest a certain numberChanging the order of elements in a listSubtract second element of element of list from other list if the first elements are equalNumber elements in a listSelect first element in deepest list of each nested listHow to repeat each element in a list and the whole list as well?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2












$begingroup$


I'm trying to repeat each element of a list x number of times, where x is the corresponding element of the same position in another list.



For example, I have list A = 1,2,3,4 and another list B = 3,1,4,2 and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4.



How do I get C from A and B?



Thanks.










share|improve this question









$endgroup$













  • $begingroup$
    What have you tried?
    $endgroup$
    – Edmund
    8 hours ago

















2












$begingroup$


I'm trying to repeat each element of a list x number of times, where x is the corresponding element of the same position in another list.



For example, I have list A = 1,2,3,4 and another list B = 3,1,4,2 and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4.



How do I get C from A and B?



Thanks.










share|improve this question









$endgroup$













  • $begingroup$
    What have you tried?
    $endgroup$
    – Edmund
    8 hours ago













2












2








2





$begingroup$


I'm trying to repeat each element of a list x number of times, where x is the corresponding element of the same position in another list.



For example, I have list A = 1,2,3,4 and another list B = 3,1,4,2 and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4.



How do I get C from A and B?



Thanks.










share|improve this question









$endgroup$




I'm trying to repeat each element of a list x number of times, where x is the corresponding element of the same position in another list.



For example, I have list A = 1,2,3,4 and another list B = 3,1,4,2 and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4.



How do I get C from A and B?



Thanks.







list-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









JinJin

564 bronze badges




564 bronze badges














  • $begingroup$
    What have you tried?
    $endgroup$
    – Edmund
    8 hours ago
















  • $begingroup$
    What have you tried?
    $endgroup$
    – Edmund
    8 hours ago















$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago




$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago










4 Answers
4






active

oldest

votes


















7














$begingroup$

Join @@ MapThread[Table, A,B]



1, 1, 1, 2, 3, 3, 3, 3, 4, 4




Join @@ Table @@@ Transpose @ A,B



1, 1, 1, 2, 3, 3, 3, 3, 4, 4




Join @@ MapThread[ConstantArray, A, B]



1, 1, 1, 2, 3, 3, 3, 3, 4, 4




Also



Internal`RepetitionFromMultiplicity @ Transpose[A, B]



1, 1, 1, 2, 3, 3, 3, 3, 4, 4







share|improve this answer











$endgroup$






















    2














    $begingroup$

    a = 1, 2, 3, 4;

    b = 3, 1, 4, 2;


    Using ConstantArray



    c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
    Transpose[a, b]]

    (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)


    or using Table



    c = Flatten[Table[#[[1]], #[[2]]] & /@
    Transpose[a, b]]

    (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)





    share|improve this answer









    $endgroup$






















      1














      $begingroup$

      Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.



      f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]


      Making this function Listable allows very simple calling method:



      f[a, b] // Flatten
      1, 1, 1, 2, 3, 3, 3, 3, 4, 4





      share|improve this answer









      $endgroup$






















        0














        $begingroup$

        a = 1, 2, 3, 4;
        b = 3, 1, 4, 2;

        Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
        (* or *)
        Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]



        1, 1, 1, 2, 3, 3, 3, 3, 4, 4




        Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]



        1, 1, 1, 2, 3, 3, 3, 3, 4, 4







        share|improve this answer









        $endgroup$

















          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );














          draft saved

          draft discarded
















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206438%2frepeat-elements-in-list-but-the-number-of-times-each-element-is-repeated-is-pro%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          $begingroup$

          Join @@ MapThread[Table, A,B]



          1, 1, 1, 2, 3, 3, 3, 3, 4, 4




          Join @@ Table @@@ Transpose @ A,B



          1, 1, 1, 2, 3, 3, 3, 3, 4, 4




          Join @@ MapThread[ConstantArray, A, B]



          1, 1, 1, 2, 3, 3, 3, 3, 4, 4




          Also



          Internal`RepetitionFromMultiplicity @ Transpose[A, B]



          1, 1, 1, 2, 3, 3, 3, 3, 4, 4







          share|improve this answer











          $endgroup$



















            7














            $begingroup$

            Join @@ MapThread[Table, A,B]



            1, 1, 1, 2, 3, 3, 3, 3, 4, 4




            Join @@ Table @@@ Transpose @ A,B



            1, 1, 1, 2, 3, 3, 3, 3, 4, 4




            Join @@ MapThread[ConstantArray, A, B]



            1, 1, 1, 2, 3, 3, 3, 3, 4, 4




            Also



            Internal`RepetitionFromMultiplicity @ Transpose[A, B]



            1, 1, 1, 2, 3, 3, 3, 3, 4, 4







            share|improve this answer











            $endgroup$

















              7














              7










              7







              $begingroup$

              Join @@ MapThread[Table, A,B]



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4




              Join @@ Table @@@ Transpose @ A,B



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4




              Join @@ MapThread[ConstantArray, A, B]



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4




              Also



              Internal`RepetitionFromMultiplicity @ Transpose[A, B]



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4







              share|improve this answer











              $endgroup$



              Join @@ MapThread[Table, A,B]



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4




              Join @@ Table @@@ Transpose @ A,B



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4




              Join @@ MapThread[ConstantArray, A, B]



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4




              Also



              Internal`RepetitionFromMultiplicity @ Transpose[A, B]



              1, 1, 1, 2, 3, 3, 3, 3, 4, 4








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 4 hours ago

























              answered 7 hours ago









              kglrkglr

              217k10 gold badges247 silver badges497 bronze badges




              217k10 gold badges247 silver badges497 bronze badges


























                  2














                  $begingroup$

                  a = 1, 2, 3, 4;

                  b = 3, 1, 4, 2;


                  Using ConstantArray



                  c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
                  Transpose[a, b]]

                  (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)


                  or using Table



                  c = Flatten[Table[#[[1]], #[[2]]] & /@
                  Transpose[a, b]]

                  (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)





                  share|improve this answer









                  $endgroup$



















                    2














                    $begingroup$

                    a = 1, 2, 3, 4;

                    b = 3, 1, 4, 2;


                    Using ConstantArray



                    c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
                    Transpose[a, b]]

                    (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)


                    or using Table



                    c = Flatten[Table[#[[1]], #[[2]]] & /@
                    Transpose[a, b]]

                    (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)





                    share|improve this answer









                    $endgroup$

















                      2














                      2










                      2







                      $begingroup$

                      a = 1, 2, 3, 4;

                      b = 3, 1, 4, 2;


                      Using ConstantArray



                      c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
                      Transpose[a, b]]

                      (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)


                      or using Table



                      c = Flatten[Table[#[[1]], #[[2]]] & /@
                      Transpose[a, b]]

                      (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)





                      share|improve this answer









                      $endgroup$



                      a = 1, 2, 3, 4;

                      b = 3, 1, 4, 2;


                      Using ConstantArray



                      c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
                      Transpose[a, b]]

                      (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)


                      or using Table



                      c = Flatten[Table[#[[1]], #[[2]]] & /@
                      Transpose[a, b]]

                      (* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 7 hours ago









                      Bob HanlonBob Hanlon

                      65.6k3 gold badges37 silver badges100 bronze badges




                      65.6k3 gold badges37 silver badges100 bronze badges
























                          1














                          $begingroup$

                          Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.



                          f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]


                          Making this function Listable allows very simple calling method:



                          f[a, b] // Flatten
                          1, 1, 1, 2, 3, 3, 3, 3, 4, 4





                          share|improve this answer









                          $endgroup$



















                            1














                            $begingroup$

                            Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.



                            f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]


                            Making this function Listable allows very simple calling method:



                            f[a, b] // Flatten
                            1, 1, 1, 2, 3, 3, 3, 3, 4, 4





                            share|improve this answer









                            $endgroup$

















                              1














                              1










                              1







                              $begingroup$

                              Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.



                              f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]


                              Making this function Listable allows very simple calling method:



                              f[a, b] // Flatten
                              1, 1, 1, 2, 3, 3, 3, 3, 4, 4





                              share|improve this answer









                              $endgroup$



                              Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.



                              f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]


                              Making this function Listable allows very simple calling method:



                              f[a, b] // Flatten
                              1, 1, 1, 2, 3, 3, 3, 3, 4, 4






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              bill sbill s

                              56.4k3 gold badges80 silver badges161 bronze badges




                              56.4k3 gold badges80 silver badges161 bronze badges
























                                  0














                                  $begingroup$

                                  a = 1, 2, 3, 4;
                                  b = 3, 1, 4, 2;

                                  Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
                                  (* or *)
                                  Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]



                                  1, 1, 1, 2, 3, 3, 3, 3, 4, 4




                                  Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]



                                  1, 1, 1, 2, 3, 3, 3, 3, 4, 4







                                  share|improve this answer









                                  $endgroup$



















                                    0














                                    $begingroup$

                                    a = 1, 2, 3, 4;
                                    b = 3, 1, 4, 2;

                                    Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
                                    (* or *)
                                    Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]



                                    1, 1, 1, 2, 3, 3, 3, 3, 4, 4




                                    Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]



                                    1, 1, 1, 2, 3, 3, 3, 3, 4, 4







                                    share|improve this answer









                                    $endgroup$

















                                      0














                                      0










                                      0







                                      $begingroup$

                                      a = 1, 2, 3, 4;
                                      b = 3, 1, 4, 2;

                                      Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
                                      (* or *)
                                      Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]



                                      1, 1, 1, 2, 3, 3, 3, 3, 4, 4




                                      Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]



                                      1, 1, 1, 2, 3, 3, 3, 3, 4, 4







                                      share|improve this answer









                                      $endgroup$



                                      a = 1, 2, 3, 4;
                                      b = 3, 1, 4, 2;

                                      Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
                                      (* or *)
                                      Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]



                                      1, 1, 1, 2, 3, 3, 3, 3, 4, 4




                                      Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]



                                      1, 1, 1, 2, 3, 3, 3, 3, 4, 4








                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 1 hour ago









                                      MelaGoMelaGo

                                      2,8111 gold badge2 silver badges8 bronze badges




                                      2,8111 gold badge2 silver badges8 bronze badges































                                          draft saved

                                          draft discarded















































                                          Thanks for contributing an answer to Mathematica Stack Exchange!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          Use MathJax to format equations. MathJax reference.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206438%2frepeat-elements-in-list-but-the-number-of-times-each-element-is-repeated-is-pro%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                          François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480