Map a function that takes arguments in different levels of a listMap a function across a list...

Can a human variant take proficiency in initiative?

Why do we need explainable AI?

How would a disabled person earn their living in a medieval-type town?

How to update standalone ESXi / PowerEdge server?

How to say "too quickly", "too recklessly" etc

Different past tense for various *et words

Can the Tasha's Hideous Laughter spell affect a deaf creature?

Using large parts of a research paper

Can UV radiation be safe for the skin?

How did Gollum know Sauron was gathering the Haradrim to make war?

Polarity of gas discharge tubes?

How to find better food in airports

garage light with two hots and one neutral

Can a country avoid prosecution for crimes against humanity by denying it happened?

Divide Numbers by 0

Calculate Landau's function

Why don't "echo -e" commands seem to produce the right output?

What are the French equivalents of "blow away the cobwebs"?

Are there consequences for not filing a DMCA (any country)

How to solve this inequality , when there is a irrational power?

Would there be balance issues if I allowed opportunity attacks against any creature, not just hostile ones?

Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code

Table alignment (make the content centre)

When did Gwenpool meet Ms. Marvel?



Map a function that takes arguments in different levels of a list


Map a function across a list conditionallyMap a function over the columns of an M x N arraySpan a function across several consecutive elements in a listUsing MapIndexed only at certain elements of a listMapping a function over n levelsMap an Argument to a list of functionsA list of functions taking different argumentsApply a function to a list that is a subset of sublistsMap function over two lists






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







4












$begingroup$


I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
So for example, this is my list of lists:




{{4,1}, {3,1,1}, {2,2,1}}




I would like to map the function to the list of lists to obtain the following outcome:




{ {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}




I would appreciate it if someone could tell me how to do it. Thank you in advance!










share|improve this question







New contributor



Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$





















    4












    $begingroup$


    I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
    So for example, this is my list of lists:




    {{4,1}, {3,1,1}, {2,2,1}}




    I would like to map the function to the list of lists to obtain the following outcome:




    { {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}




    I would appreciate it if someone could tell me how to do it. Thank you in advance!










    share|improve this question







    New contributor



    Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$

















      4












      4








      4





      $begingroup$


      I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
      So for example, this is my list of lists:




      {{4,1}, {3,1,1}, {2,2,1}}




      I would like to map the function to the list of lists to obtain the following outcome:




      { {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}




      I would appreciate it if someone could tell me how to do it. Thank you in advance!










      share|improve this question







      New contributor



      Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$




      I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
      So for example, this is my list of lists:




      {{4,1}, {3,1,1}, {2,2,1}}




      I would like to map the function to the list of lists to obtain the following outcome:




      { {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}




      I would appreciate it if someone could tell me how to do it. Thank you in advance!







      map






      share|improve this question







      New contributor



      Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question







      New contributor



      Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question






      New contributor



      Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      MavatanetMavatanet

      261 bronze badge




      261 bronze badge




      New contributor



      Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      Mavatanet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          3 Answers
          3






          active

          oldest

          votes


















          3













          $begingroup$

          lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
          Table[Table[f[i, j], {j, i}], {i, lst}]



          {{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
          f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
          f[{2, 2, 1}, 1]}}







          share|improve this answer









          $endgroup$











          • 1




            $begingroup$
            +1. also: Table[f[i, j], {i, lst}, {j, i}]
            $endgroup$
            – WReach
            44 mins ago



















          3













          $begingroup$

          ClearAll[g1,  f]
          g1 = Function[x, f[x, #] & /@ x]

          lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};

          g1 /@ lst



          {{f[{4, 1}, 4], f[{4, 1}, 1]},

          {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

          {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




          Also



          ClearAll[g2]
          g2[x_]:= Map[f[x, #] &] @ x

          g2 /@ lst



          {{f[{4, 1}, 4], f[{4, 1}, 1]},

          {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

          {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




          and



          ClearAll[g3]
          g3 = Thread[f[#, #], List, {2}] &

          g3 /@ lst



          {{f[{4, 1}, 4], f[{4, 1}, 1]},

          {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

          {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}







          share|improve this answer











          $endgroup$























            0













            $begingroup$

            I like the techniques in the other answers (particularly the Table approach in the answer by MelaGo), but here are a few other options to add to the mix...



            Given:



            $list = {{4,1}, {3,1,1}, {2,2,1}};


            Then any of the following expressions yield the desired result:



            Curry[f, 2][#] /@ # & /@ $list


            or



            Cases[$list, l_ :> (f[l, #] & /@ l)]


            or



            Replace[$list, l_ :> (f[l, #] & /@ l), {1}]


            or



            MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]


            or



            ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]





            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/3.0/"u003ecc by-sa 3.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
              });


              }
              });






              Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f204657%2fmap-a-function-that-takes-arguments-in-different-levels-of-a-list%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









              3













              $begingroup$

              lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
              Table[Table[f[i, j], {j, i}], {i, lst}]



              {{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
              f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
              f[{2, 2, 1}, 1]}}







              share|improve this answer









              $endgroup$











              • 1




                $begingroup$
                +1. also: Table[f[i, j], {i, lst}, {j, i}]
                $endgroup$
                – WReach
                44 mins ago
















              3













              $begingroup$

              lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
              Table[Table[f[i, j], {j, i}], {i, lst}]



              {{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
              f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
              f[{2, 2, 1}, 1]}}







              share|improve this answer









              $endgroup$











              • 1




                $begingroup$
                +1. also: Table[f[i, j], {i, lst}, {j, i}]
                $endgroup$
                – WReach
                44 mins ago














              3














              3










              3







              $begingroup$

              lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
              Table[Table[f[i, j], {j, i}], {i, lst}]



              {{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
              f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
              f[{2, 2, 1}, 1]}}







              share|improve this answer









              $endgroup$



              lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
              Table[Table[f[i, j], {j, i}], {i, lst}]



              {{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
              f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
              f[{2, 2, 1}, 1]}}








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 8 hours ago









              MelaGoMelaGo

              2,6361 gold badge1 silver badge7 bronze badges




              2,6361 gold badge1 silver badge7 bronze badges











              • 1




                $begingroup$
                +1. also: Table[f[i, j], {i, lst}, {j, i}]
                $endgroup$
                – WReach
                44 mins ago














              • 1




                $begingroup$
                +1. also: Table[f[i, j], {i, lst}, {j, i}]
                $endgroup$
                – WReach
                44 mins ago








              1




              1




              $begingroup$
              +1. also: Table[f[i, j], {i, lst}, {j, i}]
              $endgroup$
              – WReach
              44 mins ago




              $begingroup$
              +1. also: Table[f[i, j], {i, lst}, {j, i}]
              $endgroup$
              – WReach
              44 mins ago













              3













              $begingroup$

              ClearAll[g1,  f]
              g1 = Function[x, f[x, #] & /@ x]

              lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};

              g1 /@ lst



              {{f[{4, 1}, 4], f[{4, 1}, 1]},

              {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

              {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




              Also



              ClearAll[g2]
              g2[x_]:= Map[f[x, #] &] @ x

              g2 /@ lst



              {{f[{4, 1}, 4], f[{4, 1}, 1]},

              {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

              {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




              and



              ClearAll[g3]
              g3 = Thread[f[#, #], List, {2}] &

              g3 /@ lst



              {{f[{4, 1}, 4], f[{4, 1}, 1]},

              {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

              {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}







              share|improve this answer











              $endgroup$




















                3













                $begingroup$

                ClearAll[g1,  f]
                g1 = Function[x, f[x, #] & /@ x]

                lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};

                g1 /@ lst



                {{f[{4, 1}, 4], f[{4, 1}, 1]},

                {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




                Also



                ClearAll[g2]
                g2[x_]:= Map[f[x, #] &] @ x

                g2 /@ lst



                {{f[{4, 1}, 4], f[{4, 1}, 1]},

                {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




                and



                ClearAll[g3]
                g3 = Thread[f[#, #], List, {2}] &

                g3 /@ lst



                {{f[{4, 1}, 4], f[{4, 1}, 1]},

                {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}







                share|improve this answer











                $endgroup$


















                  3














                  3










                  3







                  $begingroup$

                  ClearAll[g1,  f]
                  g1 = Function[x, f[x, #] & /@ x]

                  lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};

                  g1 /@ lst



                  {{f[{4, 1}, 4], f[{4, 1}, 1]},

                  {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                  {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




                  Also



                  ClearAll[g2]
                  g2[x_]:= Map[f[x, #] &] @ x

                  g2 /@ lst



                  {{f[{4, 1}, 4], f[{4, 1}, 1]},

                  {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                  {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




                  and



                  ClearAll[g3]
                  g3 = Thread[f[#, #], List, {2}] &

                  g3 /@ lst



                  {{f[{4, 1}, 4], f[{4, 1}, 1]},

                  {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                  {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}







                  share|improve this answer











                  $endgroup$



                  ClearAll[g1,  f]
                  g1 = Function[x, f[x, #] & /@ x]

                  lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};

                  g1 /@ lst



                  {{f[{4, 1}, 4], f[{4, 1}, 1]},

                  {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                  {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




                  Also



                  ClearAll[g2]
                  g2[x_]:= Map[f[x, #] &] @ x

                  g2 /@ lst



                  {{f[{4, 1}, 4], f[{4, 1}, 1]},

                  {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                  {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}




                  and



                  ClearAll[g3]
                  g3 = Thread[f[#, #], List, {2}] &

                  g3 /@ lst



                  {{f[{4, 1}, 4], f[{4, 1}, 1]},

                  {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},

                  {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 7 hours ago

























                  answered 8 hours ago









                  kglrkglr

                  215k10 gold badges245 silver badges490 bronze badges




                  215k10 gold badges245 silver badges490 bronze badges


























                      0













                      $begingroup$

                      I like the techniques in the other answers (particularly the Table approach in the answer by MelaGo), but here are a few other options to add to the mix...



                      Given:



                      $list = {{4,1}, {3,1,1}, {2,2,1}};


                      Then any of the following expressions yield the desired result:



                      Curry[f, 2][#] /@ # & /@ $list


                      or



                      Cases[$list, l_ :> (f[l, #] & /@ l)]


                      or



                      Replace[$list, l_ :> (f[l, #] & /@ l), {1}]


                      or



                      MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]


                      or



                      ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]





                      share|improve this answer









                      $endgroup$




















                        0













                        $begingroup$

                        I like the techniques in the other answers (particularly the Table approach in the answer by MelaGo), but here are a few other options to add to the mix...



                        Given:



                        $list = {{4,1}, {3,1,1}, {2,2,1}};


                        Then any of the following expressions yield the desired result:



                        Curry[f, 2][#] /@ # & /@ $list


                        or



                        Cases[$list, l_ :> (f[l, #] & /@ l)]


                        or



                        Replace[$list, l_ :> (f[l, #] & /@ l), {1}]


                        or



                        MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]


                        or



                        ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]





                        share|improve this answer









                        $endgroup$


















                          0














                          0










                          0







                          $begingroup$

                          I like the techniques in the other answers (particularly the Table approach in the answer by MelaGo), but here are a few other options to add to the mix...



                          Given:



                          $list = {{4,1}, {3,1,1}, {2,2,1}};


                          Then any of the following expressions yield the desired result:



                          Curry[f, 2][#] /@ # & /@ $list


                          or



                          Cases[$list, l_ :> (f[l, #] & /@ l)]


                          or



                          Replace[$list, l_ :> (f[l, #] & /@ l), {1}]


                          or



                          MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]


                          or



                          ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]





                          share|improve this answer









                          $endgroup$



                          I like the techniques in the other answers (particularly the Table approach in the answer by MelaGo), but here are a few other options to add to the mix...



                          Given:



                          $list = {{4,1}, {3,1,1}, {2,2,1}};


                          Then any of the following expressions yield the desired result:



                          Curry[f, 2][#] /@ # & /@ $list


                          or



                          Cases[$list, l_ :> (f[l, #] & /@ l)]


                          or



                          Replace[$list, l_ :> (f[l, #] & /@ l), {1}]


                          or



                          MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]


                          or



                          ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 23 mins ago









                          WReachWReach

                          55.1k2 gold badges119 silver badges219 bronze badges




                          55.1k2 gold badges119 silver badges219 bronze badges

























                              Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.













                              Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.












                              Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.
















                              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%2f204657%2fmap-a-function-that-takes-arguments-in-different-levels-of-a-list%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

                              Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                              Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                              Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m