Truncate a set of lists until a subset of entries matches?
Consider lists of vectors of elements like
list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};
where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.
I would like to have a function truncate[listoflists_]
that takes all these lists and truncates them to only have the vectors containing ai,bi
entries that are present in all of them:
{list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
list[1]
list[2]
list[3]
{{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}
{{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}
{{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}
Is there a quick way to do this in Mathematica? Thanks for any suggestion.
list-manipulation function-construction
add a comment |
Consider lists of vectors of elements like
list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};
where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.
I would like to have a function truncate[listoflists_]
that takes all these lists and truncates them to only have the vectors containing ai,bi
entries that are present in all of them:
{list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
list[1]
list[2]
list[3]
{{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}
{{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}
{{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}
Is there a quick way to do this in Mathematica? Thanks for any suggestion.
list-manipulation function-construction
add a comment |
Consider lists of vectors of elements like
list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};
where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.
I would like to have a function truncate[listoflists_]
that takes all these lists and truncates them to only have the vectors containing ai,bi
entries that are present in all of them:
{list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
list[1]
list[2]
list[3]
{{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}
{{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}
{{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}
Is there a quick way to do this in Mathematica? Thanks for any suggestion.
list-manipulation function-construction
Consider lists of vectors of elements like
list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};
where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.
I would like to have a function truncate[listoflists_]
that takes all these lists and truncates them to only have the vectors containing ai,bi
entries that are present in all of them:
{list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
list[1]
list[2]
list[3]
{{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}
{{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}
{{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}
Is there a quick way to do this in Mathematica? Thanks for any suggestion.
list-manipulation function-construction
list-manipulation function-construction
asked Nov 23 '18 at 21:04
KagaratschKagaratsch
4,59231246
4,59231246
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It is short (does not mean quick) to get it via associations:
truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &
We convert {a1,b1,c11}
to {a1,b1}->{a1,b1,c11}
and everything else is straightforward.
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I have changed it totruncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.
– Kagaratsch
Nov 23 '18 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
add a comment |
Also
tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&
lists = list /@ {1, 2, 3};
tF[lists, {1, 2}]
{{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},
{{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},
{{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}
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%2f186600%2ftruncate-a-set-of-lists-until-a-subset-of-entries-matches%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is short (does not mean quick) to get it via associations:
truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &
We convert {a1,b1,c11}
to {a1,b1}->{a1,b1,c11}
and everything else is straightforward.
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I have changed it totruncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.
– Kagaratsch
Nov 23 '18 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
add a comment |
It is short (does not mean quick) to get it via associations:
truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &
We convert {a1,b1,c11}
to {a1,b1}->{a1,b1,c11}
and everything else is straightforward.
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I have changed it totruncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.
– Kagaratsch
Nov 23 '18 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
add a comment |
It is short (does not mean quick) to get it via associations:
truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &
We convert {a1,b1,c11}
to {a1,b1}->{a1,b1,c11}
and everything else is straightforward.
It is short (does not mean quick) to get it via associations:
truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &
We convert {a1,b1,c11}
to {a1,b1}->{a1,b1,c11}
and everything else is straightforward.
edited Nov 24 '18 at 0:27
answered Nov 23 '18 at 21:23
Kuba♦Kuba
104k12201518
104k12201518
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I have changed it totruncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.
– Kagaratsch
Nov 23 '18 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
add a comment |
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I have changed it totruncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.
– Kagaratsch
Nov 23 '18 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
1
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 '18 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 '18 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 '18 at 22:00
I have changed it to
truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.– Kagaratsch
Nov 23 '18 at 23:35
I have changed it to
truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] &
to make it compatible with longer vector cases.– Kagaratsch
Nov 23 '18 at 23:35
1
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 '18 at 0:27
add a comment |
Also
tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&
lists = list /@ {1, 2, 3};
tF[lists, {1, 2}]
{{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},
{{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},
{{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}
add a comment |
Also
tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&
lists = list /@ {1, 2, 3};
tF[lists, {1, 2}]
{{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},
{{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},
{{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}
add a comment |
Also
tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&
lists = list /@ {1, 2, 3};
tF[lists, {1, 2}]
{{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},
{{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},
{{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}
Also
tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&
lists = list /@ {1, 2, 3};
tF[lists, {1, 2}]
{{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},
{{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},
{{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}
answered Nov 24 '18 at 19:32
kglrkglr
178k9198409
178k9198409
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%2f186600%2ftruncate-a-set-of-lists-until-a-subset-of-entries-matches%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