I want to extract a specific element from the list and add it
{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
add a comment |
{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
add a comment |
{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
{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
list-manipulation
edited Jan 8 at 6:21
Αλέξανδρος Ζεγγ
4,1441929
4,1441929
asked Jan 8 at 5:37
Kenta KawaiKenta Kawai
363
363
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
add a comment |
list= (*your list*)
Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]
(*0.692301*)
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
You can even useKeySelect
:Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
add a comment |
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
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
add a comment |
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
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
answered Jan 8 at 6:26
Αλέξανδρος ΖεγγΑλέξανδρος Ζεγγ
4,1441929
4,1441929
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
add a comment |
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
add a comment |
list= (*your list*)
Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]
(*0.692301*)
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
You can even useKeySelect
:Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
add a comment |
list= (*your list*)
Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]
(*0.692301*)
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
You can even useKeySelect
:Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
add a comment |
list= (*your list*)
Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]
(*0.692301*)
list= (*your list*)
Total[Select[list, (#[[1, 1]] + #[[1, 2]]) == 8 &] [[All, 2]]]
(*0.692301*)
answered Jan 8 at 6:57
Hubble07Hubble07
2,966721
2,966721
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
You can even useKeySelect
:Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
add a comment |
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
You can even useKeySelect
:Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
Thank you for advice.It solved properly.
– Kenta Kawai
2 days ago
You can even use
KeySelect
: Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
You can even use
KeySelect
: Total @ KeySelect[list, Plus @@ # == 8 &]
– Sjoerd Smit
2 days ago
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 2 days ago
answered 2 days ago
Mr.Wizard♦Mr.Wizard
230k294741039
230k294741039
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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