I want to extract a specific element from the list and add it












5














{p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6}


I want to extract a specific element from the list and add it.
For example, I want to extract the element which becomes 8 by the element of p [i, j] and add it.



p[0, 8] + p[1, 7] + p[2, 6] + ... = answer


Please advise an efficient way, thank you.










share|improve this question





























    5














    {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
    p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
    p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
    p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
    p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
    p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
    p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
    p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
    p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6}


    I want to extract a specific element from the list and add it.
    For example, I want to extract the element which becomes 8 by the element of p [i, j] and add it.



    p[0, 8] + p[1, 7] + p[2, 6] + ... = answer


    Please advise an efficient way, thank you.










    share|improve this question



























      5












      5








      5







      {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
      p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
      p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
      p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
      p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
      p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
      p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
      p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
      p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6}


      I want to extract a specific element from the list and add it.
      For example, I want to extract the element which becomes 8 by the element of p [i, j] and add it.



      p[0, 8] + p[1, 7] + p[2, 6] + ... = answer


      Please advise an efficient way, thank you.










      share|improve this question















      {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
      p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
      p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
      p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
      p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
      p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
      p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
      p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
      p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6}


      I want to extract a specific element from the list and add it.
      For example, I want to extract the element which becomes 8 by the element of p [i, j] and add it.



      p[0, 8] + p[1, 7] + p[2, 6] + ... = answer


      Please advise an efficient way, thank you.







      list-manipulation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Αλέξανδρος Ζεγγ

      4,1441929




      4,1441929










      asked 2 days ago









      Kenta KawaiKenta Kawai

      363




      363






















          3 Answers
          3






          active

          oldest

          votes


















          7














          First assign a name to your above list, e.g., run



          rules = (*your list*);


          Then generate the expression you want, using Table or Array



          sum = Plus @@ Table[p[i, 8 - i], {i, 0, 8}]
          sum = Array[p[#, 8 - #] &, 9, 0, Plus]



          p[0, 8] + p[1, 7] + p[2, 6] + p[3, 5] + p[4, 4] + p[5, 3] + p[6, 2] + p[7, 1] + p[8, 0]



          Finally, apply the rules to finish the value substitution via ReplaceAll (/.):



          sum /. rules



          0.692301






          share|improve this answer





















          • Thank you for advice.It solved properly.
            – Kenta Kawai
            yesterday



















          4














           list= (*your list*)

          Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]

          (*0.692301*)





          share|improve this answer





















          • Thank you for advice.It solved properly.
            – Kenta Kawai
            yesterday










          • You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
            – Sjoerd Smit
            yesterday



















          3














          Consider GroupBy or Merge. Pick is less specific but still applicable.



          rules = {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
          p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
          p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
          p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
          p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
          p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
          p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
          p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
          p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6};

          GroupBy[rules, Total@*First -> Last, Tr][8]

          Merge[Total@# -> #2 & @@@ rules, Tr][8]

          Merge[Plus @@@ # & /@ rules, Tr][8]

          Merge[rules /. p -> Plus, Tr][8]

          Pick[Values@rules, Total /@ Keys@rules, 8] // Tr


          All give




          0.692301






          share|improve this answer























            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            });
            });
            }, "mathjax-editing");

            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189026%2fi-want-to-extract-a-specific-element-from-the-list-and-add-it%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









            7














            First assign a name to your above list, e.g., run



            rules = (*your list*);


            Then generate the expression you want, using Table or Array



            sum = Plus @@ Table[p[i, 8 - i], {i, 0, 8}]
            sum = Array[p[#, 8 - #] &, 9, 0, Plus]



            p[0, 8] + p[1, 7] + p[2, 6] + p[3, 5] + p[4, 4] + p[5, 3] + p[6, 2] + p[7, 1] + p[8, 0]



            Finally, apply the rules to finish the value substitution via ReplaceAll (/.):



            sum /. rules



            0.692301






            share|improve this answer





















            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday
















            7














            First assign a name to your above list, e.g., run



            rules = (*your list*);


            Then generate the expression you want, using Table or Array



            sum = Plus @@ Table[p[i, 8 - i], {i, 0, 8}]
            sum = Array[p[#, 8 - #] &, 9, 0, Plus]



            p[0, 8] + p[1, 7] + p[2, 6] + p[3, 5] + p[4, 4] + p[5, 3] + p[6, 2] + p[7, 1] + p[8, 0]



            Finally, apply the rules to finish the value substitution via ReplaceAll (/.):



            sum /. rules



            0.692301






            share|improve this answer





















            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday














            7












            7








            7






            First assign a name to your above list, e.g., run



            rules = (*your list*);


            Then generate the expression you want, using Table or Array



            sum = Plus @@ Table[p[i, 8 - i], {i, 0, 8}]
            sum = Array[p[#, 8 - #] &, 9, 0, Plus]



            p[0, 8] + p[1, 7] + p[2, 6] + p[3, 5] + p[4, 4] + p[5, 3] + p[6, 2] + p[7, 1] + p[8, 0]



            Finally, apply the rules to finish the value substitution via ReplaceAll (/.):



            sum /. rules



            0.692301






            share|improve this answer












            First assign a name to your above list, e.g., run



            rules = (*your list*);


            Then generate the expression you want, using Table or Array



            sum = Plus @@ Table[p[i, 8 - i], {i, 0, 8}]
            sum = Array[p[#, 8 - #] &, 9, 0, Plus]



            p[0, 8] + p[1, 7] + p[2, 6] + p[3, 5] + p[4, 4] + p[5, 3] + p[6, 2] + p[7, 1] + p[8, 0]



            Finally, apply the rules to finish the value substitution via ReplaceAll (/.):



            sum /. rules



            0.692301







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Αλέξανδρος ΖεγγΑλέξανδρος Ζεγγ

            4,1441929




            4,1441929












            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday


















            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday
















            Thank you for advice.It solved properly.
            – Kenta Kawai
            yesterday




            Thank you for advice.It solved properly.
            – Kenta Kawai
            yesterday











            4














             list= (*your list*)

            Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]

            (*0.692301*)





            share|improve this answer





















            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday










            • You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
              – Sjoerd Smit
              yesterday
















            4














             list= (*your list*)

            Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]

            (*0.692301*)





            share|improve this answer





















            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday










            • You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
              – Sjoerd Smit
              yesterday














            4












            4








            4






             list= (*your list*)

            Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]

            (*0.692301*)





            share|improve this answer












             list= (*your list*)

            Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]

            (*0.692301*)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Hubble07Hubble07

            2,966721




            2,966721












            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday










            • You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
              – Sjoerd Smit
              yesterday


















            • Thank you for advice.It solved properly.
              – Kenta Kawai
              yesterday










            • You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
              – Sjoerd Smit
              yesterday
















            Thank you for advice.It solved properly.
            – Kenta Kawai
            yesterday




            Thank you for advice.It solved properly.
            – Kenta Kawai
            yesterday












            You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
            – Sjoerd Smit
            yesterday




            You can even use KeySelect: Total @ KeySelect[list, Plus @@ # == 8 &]
            – Sjoerd Smit
            yesterday











            3














            Consider GroupBy or Merge. Pick is less specific but still applicable.



            rules = {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
            p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
            p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
            p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
            p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
            p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
            p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
            p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
            p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6};

            GroupBy[rules, Total@*First -> Last, Tr][8]

            Merge[Total@# -> #2 & @@@ rules, Tr][8]

            Merge[Plus @@@ # & /@ rules, Tr][8]

            Merge[rules /. p -> Plus, Tr][8]

            Pick[Values@rules, Total /@ Keys@rules, 8] // Tr


            All give




            0.692301






            share|improve this answer




























              3














              Consider GroupBy or Merge. Pick is less specific but still applicable.



              rules = {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
              p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
              p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
              p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
              p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
              p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
              p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
              p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
              p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6};

              GroupBy[rules, Total@*First -> Last, Tr][8]

              Merge[Total@# -> #2 & @@@ rules, Tr][8]

              Merge[Plus @@@ # & /@ rules, Tr][8]

              Merge[rules /. p -> Plus, Tr][8]

              Pick[Values@rules, Total /@ Keys@rules, 8] // Tr


              All give




              0.692301






              share|improve this answer


























                3












                3








                3






                Consider GroupBy or Merge. Pick is less specific but still applicable.



                rules = {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
                p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
                p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
                p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
                p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
                p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
                p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
                p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
                p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6};

                GroupBy[rules, Total@*First -> Last, Tr][8]

                Merge[Total@# -> #2 & @@@ rules, Tr][8]

                Merge[Plus @@@ # & /@ rules, Tr][8]

                Merge[rules /. p -> Plus, Tr][8]

                Pick[Values@rules, Total /@ Keys@rules, 8] // Tr


                All give




                0.692301






                share|improve this answer














                Consider GroupBy or Merge. Pick is less specific but still applicable.



                rules = {p[0, 8] -> 0.538464, p[1, 7] -> 0.107693, p[1, 8] -> 0.161539, 
                p[2, 6] -> 0.0323078, p[2, 7] -> 0.0484617, p[2, 8] -> 0.0538464,
                p[3, 5] -> 0.00969234, p[3, 6] -> 0.0145385, p[3, 7] -> 0.0161539,
                p[4, 4] -> 0.0029077, p[4, 5] -> 0.00436155, p[4, 6] -> 0.00484617,
                p[5, 3] -> 0.000872311, p[5, 4] -> 0.00130847, p[5, 5] -> 0.00145385,
                p[6, 2] -> 0.000261693, p[6, 3] -> 0.00039254, p[6, 4] -> 0.000436155,
                p[7, 1] -> 0.000078508, p[7, 2] -> 0.000117762, p[7, 3] -> 0.000130847,
                p[8, 0] -> 0.0000235524, p[8, 1] -> 0.0000353286, p[8, 2] -> 0.000039254,
                p[9, 0] -> 0.0000176643, p[9, 1] -> 0.0000117762, p[10, 0] -> 8.83215*10^-6};

                GroupBy[rules, Total@*First -> Last, Tr][8]

                Merge[Total@# -> #2 & @@@ rules, Tr][8]

                Merge[Plus @@@ # & /@ rules, Tr][8]

                Merge[rules /. p -> Plus, Tr][8]

                Pick[Values@rules, Total /@ Keys@rules, 8] // Tr


                All give




                0.692301







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                Mr.WizardMr.Wizard

                230k294741038




                230k294741038






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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.


                    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%2f189026%2fi-want-to-extract-a-specific-element-from-the-list-and-add-it%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

                    1300-talet

                    1300-talet

                    Has there ever been an instance of an active nuclear power plant within or near a war zone?