Adding elements to some sublists of unequal lengthAdding elements in the sublistsQuickly pruning elements in...

Closest thing to Infinity Gauntlet in DnD5e

Should a grammatical article be a part of a web link anchor

Collect results of a map operation in a Map using Collectors.toMap or groupingBy

How do you translate "Don't Fear the Reaper" into Latin?

Why do previous versions of Debian packages vanish in the package repositories? (highly relevant for version-controlled system configuration)

Can you pitch an outline?

How are steel imports supposed to threaten US national security?

How does Lightning Network over TOR work?

Is the Olympic running race fair?

'Cheddar goes "good" with burgers?' Can "go" be seen as a verb of the senses?

What is this cast-iron device on my water supply pipe?

Why can I ping 10.0.0.0/8 addresses from a 192.168.1.0/24 subnet?

Conveying the idea of " judge a book by its cover" by " juger un livre par sa couverture"

When can a graph be oriented to form a Hasse diagram of a finite poset?

Installing Proprietary Windows Drivers on Linux

Can you be promoted and then fired for-cause? (Performance)

What is this plane with its thick cockpit?

What is this dial on my old SLR for?

Chain with double bond or triple bond

D&D Monsters and Copyright

How to make "acts of patience" exciting?

How long should a test wait to assume that the result remains fixed

Fill the Image Sequence Ep. 2

How to execute a project with two resources where you need three resources?



Adding elements to some sublists of unequal length


Adding elements in the sublistsQuickly pruning elements in one structured array that exist in a separate unordered arrayHow to select 3-tuples of positions from a list with variable time-steps?Eliminating elements from sublists under a global conditionThreading sublists on listSplit list based on positions contained in another listData selection by comparing elements from different sublists in a nested listList replacements






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
.everyonelovesstackoverflow{position:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;}








4














$begingroup$


Complicated title for a simple problem:
I have list



a = {{1, 3, 5, 2}, {2, 6, 2}, {3, 5, 6, 1, 2}, {4, 2}}


In which the first element of each sublist is basically an index. The following values are the actual data.
Then I want to add data to this list, e.g.,:



b = {{2, 1}, {4, 3}}


Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:



{{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}


I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.










share|improve this question











$endgroup$























    4














    $begingroup$


    Complicated title for a simple problem:
    I have list



    a = {{1, 3, 5, 2}, {2, 6, 2}, {3, 5, 6, 1, 2}, {4, 2}}


    In which the first element of each sublist is basically an index. The following values are the actual data.
    Then I want to add data to this list, e.g.,:



    b = {{2, 1}, {4, 3}}


    Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:



    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}


    I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.










    share|improve this question











    $endgroup$



















      4












      4








      4


      1



      $begingroup$


      Complicated title for a simple problem:
      I have list



      a = {{1, 3, 5, 2}, {2, 6, 2}, {3, 5, 6, 1, 2}, {4, 2}}


      In which the first element of each sublist is basically an index. The following values are the actual data.
      Then I want to add data to this list, e.g.,:



      b = {{2, 1}, {4, 3}}


      Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:



      {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}


      I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.










      share|improve this question











      $endgroup$




      Complicated title for a simple problem:
      I have list



      a = {{1, 3, 5, 2}, {2, 6, 2}, {3, 5, 6, 1, 2}, {4, 2}}


      In which the first element of each sublist is basically an index. The following values are the actual data.
      Then I want to add data to this list, e.g.,:



      b = {{2, 1}, {4, 3}}


      Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:



      {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}


      I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.







      list-manipulation symbolic






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question



      share|improve this question








      edited 8 hours ago









      Vitaliy Kaurov

      59k6 gold badges166 silver badges286 bronze badges




      59k6 gold badges166 silver badges286 bronze badges










      asked 9 hours ago









      Mockup DungeonMockup Dungeon

      9176 silver badges13 bronze badges




      9176 silver badges13 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          5
















          $begingroup$

          Fold[Insert[#1, Last[#2], {First[#2], -1}] &, a, b]





          share|improve this answer










          $endgroup$























            2
















            $begingroup$

            Perhaps something like this:



            ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]


            If data are large and speedup is needed you might want to try Join



            ReplacePart[a, #1 -> Join[a[[#1]], {#2}] & @@@ b]


            In case you want value of a to be the new list with added elements, you whether can use AppendTo:



            ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]


            or simply reassign:



            a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]





            share|improve this answer












            $endgroup$























              1
















              $begingroup$

              You can make b into a list of rules



              rules = {#, a__} :> {#, a, #2} & @@@ b;


              and use it with ReplaceAll:



              a /. rules



              {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




              or with Replace:



              Replace[a, rules, All]



              {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




              This approach works for any ordering of the elements in the input list:



              c = RandomSample[a]



              {{2, 6, 2}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2}}




              c /. rules



              {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




              Replace[c, rules, All]



              {{2, 6, 2, 1}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2, 3}}







              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%2f207226%2fadding-elements-to-some-sublists-of-unequal-length%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                5
















                $begingroup$

                Fold[Insert[#1, Last[#2], {First[#2], -1}] &, a, b]





                share|improve this answer










                $endgroup$




















                  5
















                  $begingroup$

                  Fold[Insert[#1, Last[#2], {First[#2], -1}] &, a, b]





                  share|improve this answer










                  $endgroup$


















                    5














                    5










                    5







                    $begingroup$

                    Fold[Insert[#1, Last[#2], {First[#2], -1}] &, a, b]





                    share|improve this answer










                    $endgroup$



                    Fold[Insert[#1, Last[#2], {First[#2], -1}] &, a, b]






                    share|improve this answer













                    share|improve this answer




                    share|improve this answer



                    share|improve this answer










                    answered 8 hours ago









                    Suba ThomasSuba Thomas

                    4,05611 silver badges20 bronze badges




                    4,05611 silver badges20 bronze badges




























                        2
















                        $begingroup$

                        Perhaps something like this:



                        ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]


                        If data are large and speedup is needed you might want to try Join



                        ReplacePart[a, #1 -> Join[a[[#1]], {#2}] & @@@ b]


                        In case you want value of a to be the new list with added elements, you whether can use AppendTo:



                        ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]


                        or simply reassign:



                        a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]





                        share|improve this answer












                        $endgroup$




















                          2
















                          $begingroup$

                          Perhaps something like this:



                          ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]


                          If data are large and speedup is needed you might want to try Join



                          ReplacePart[a, #1 -> Join[a[[#1]], {#2}] & @@@ b]


                          In case you want value of a to be the new list with added elements, you whether can use AppendTo:



                          ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]


                          or simply reassign:



                          a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]





                          share|improve this answer












                          $endgroup$


















                            2














                            2










                            2







                            $begingroup$

                            Perhaps something like this:



                            ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]


                            If data are large and speedup is needed you might want to try Join



                            ReplacePart[a, #1 -> Join[a[[#1]], {#2}] & @@@ b]


                            In case you want value of a to be the new list with added elements, you whether can use AppendTo:



                            ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]


                            or simply reassign:



                            a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]





                            share|improve this answer












                            $endgroup$



                            Perhaps something like this:



                            ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]


                            If data are large and speedup is needed you might want to try Join



                            ReplacePart[a, #1 -> Join[a[[#1]], {#2}] & @@@ b]


                            In case you want value of a to be the new list with added elements, you whether can use AppendTo:



                            ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]


                            or simply reassign:



                            a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]






                            share|improve this answer















                            share|improve this answer




                            share|improve this answer



                            share|improve this answer








                            edited 8 hours ago

























                            answered 8 hours ago









                            Vitaliy KaurovVitaliy Kaurov

                            59k6 gold badges166 silver badges286 bronze badges




                            59k6 gold badges166 silver badges286 bronze badges


























                                1
















                                $begingroup$

                                You can make b into a list of rules



                                rules = {#, a__} :> {#, a, #2} & @@@ b;


                                and use it with ReplaceAll:



                                a /. rules



                                {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                or with Replace:



                                Replace[a, rules, All]



                                {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                This approach works for any ordering of the elements in the input list:



                                c = RandomSample[a]



                                {{2, 6, 2}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2}}




                                c /. rules



                                {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                Replace[c, rules, All]



                                {{2, 6, 2, 1}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2, 3}}







                                share|improve this answer












                                $endgroup$




















                                  1
















                                  $begingroup$

                                  You can make b into a list of rules



                                  rules = {#, a__} :> {#, a, #2} & @@@ b;


                                  and use it with ReplaceAll:



                                  a /. rules



                                  {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                  or with Replace:



                                  Replace[a, rules, All]



                                  {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                  This approach works for any ordering of the elements in the input list:



                                  c = RandomSample[a]



                                  {{2, 6, 2}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2}}




                                  c /. rules



                                  {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                  Replace[c, rules, All]



                                  {{2, 6, 2, 1}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2, 3}}







                                  share|improve this answer












                                  $endgroup$


















                                    1














                                    1










                                    1







                                    $begingroup$

                                    You can make b into a list of rules



                                    rules = {#, a__} :> {#, a, #2} & @@@ b;


                                    and use it with ReplaceAll:



                                    a /. rules



                                    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                    or with Replace:



                                    Replace[a, rules, All]



                                    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                    This approach works for any ordering of the elements in the input list:



                                    c = RandomSample[a]



                                    {{2, 6, 2}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2}}




                                    c /. rules



                                    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                    Replace[c, rules, All]



                                    {{2, 6, 2, 1}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2, 3}}







                                    share|improve this answer












                                    $endgroup$



                                    You can make b into a list of rules



                                    rules = {#, a__} :> {#, a, #2} & @@@ b;


                                    and use it with ReplaceAll:



                                    a /. rules



                                    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                    or with Replace:



                                    Replace[a, rules, All]



                                    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                    This approach works for any ordering of the elements in the input list:



                                    c = RandomSample[a]



                                    {{2, 6, 2}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2}}




                                    c /. rules



                                    {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}




                                    Replace[c, rules, All]



                                    {{2, 6, 2, 1}, {3, 5, 6, 1, 2}, {1, 3, 5, 2}, {4, 2, 3}}








                                    share|improve this answer















                                    share|improve this answer




                                    share|improve this answer



                                    share|improve this answer








                                    edited 7 hours ago

























                                    answered 8 hours ago









                                    kglrkglr

                                    220k10 gold badges250 silver badges504 bronze badges




                                    220k10 gold badges250 silver badges504 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%2f207226%2fadding-elements-to-some-sublists-of-unequal-length%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

                                        Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                                        The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                                        Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...