Truncate a set of lists until a subset of entries matches?
up vote
3
down vote
favorite
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 |
up vote
3
down vote
favorite
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 |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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 at 21:04
Kagaratsch
4,57731246
4,57731246
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
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 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 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 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
add a comment |
up vote
1
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 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 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
add a comment |
up vote
3
down vote
accepted
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 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 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 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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 at 0:27
answered Nov 23 at 21:23
Kuba♦
102k12200511
102k12200511
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 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 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
add a comment |
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 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 at 23:35
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
1
1
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 at 21:45
Love it when a mod ends up in the LQ review. :)
– Michael E2
Nov 23 at 21:45
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
@MichaelE2 ok, explanation added.
– Kuba♦
Nov 23 at 21:53
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 at 22:00
I approved it anyway. It's not always clear that excessive exposition would be an improvement.
– Michael E2
Nov 23 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 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 at 23:35
1
1
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
@Kagaratsch right, you mentioned it. I edited the code.
– Kuba♦
Nov 24 at 0:27
add a comment |
up vote
1
down vote
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 |
up vote
1
down vote
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 |
up vote
1
down vote
up vote
1
down vote
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 at 19:32
kglr
173k8196401
173k8196401
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.
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.
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