Extracting sublists that contain similar elementsPrepend 0 to sublistsComparing elements of the $n^textth$ sublist in a ragged list with the $n^textth$ member of a sequenceSelect sublists with second element nearest to zeroAdd elements of one list to sublists of another listSplit list into overlapping sublistsEliminating elements from sublists under a global conditionSort columns in a TableFormList manipulation with random elements under limiting conditionsHow to split sublists into sub-sublists without merging the sublistsProblems in dealing with sublists
Why does this if-statement combining assignment and an equality check return true?
A steel cutting sword?
I know that there is a preselected candidate for a position to be filled at my department. What should I do?
Parallel fifths in the orchestra
Does COBRA make sense anymore with the ACA?
How to politely tell someone they did not hit "reply to all" in an email?
Do I need full recovery mode when I have multiple daily backup?
Can a US state pass/enforce a law stating that a group of people owe money to the state government?
Why aren't space telescopes put in GEO?
Compaq Portable vs IBM 5155 Portable PC
Is this statement about cut time correct?
Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?
Defining the standard model of PA so that a space alien could understand
What does $!# mean in Shell scripting?
Can I connect my older mathematica front-end to the free wolfram engine?
In the 3D Zeldas, is it faster to roll or to simply walk?
Is "cool" appropriate or offensive to use in IMs?
Can a person survive on blood in place of water?
Why are GND pads often only connected by four traces?
Why did the person in charge of a principality not just declare themself king?
Where's this lookout in Nova Scotia?
Is the Indo-European language family made up?
Best material to absorb as much light as possible
Count Even Digits In Number
Extracting sublists that contain similar elements
Prepend 0 to sublistsComparing elements of the $n^textth$ sublist in a ragged list with the $n^textth$ member of a sequenceSelect sublists with second element nearest to zeroAdd elements of one list to sublists of another listSplit list into overlapping sublistsEliminating elements from sublists under a global conditionSort columns in a TableFormList manipulation with random elements under limiting conditionsHow to split sublists into sub-sublists without merging the sublistsProblems in dealing with sublists
$begingroup$
Let's say I have a list containing different sized sublists of string pairs structured like this
"A", "B", "C", "D", "E", "D", "G","B", "H", "B", "I", "B"
where the first and second sublists each contain two string pairs and the third and fourth sublists each contain 1 string pair.
I want to get rid of every string pair where the second string of the string pair does not appear in each sublist. For example, there exists string pairs in each sublist where the second string of the string pair is "B". Thus, I only want to keep those string pairs. The final list should look like this
"A", "B", "G","B", "H", "B", "I", "B"
Although each of the first two sublists contain a pair of strings where there is a "D" in the second element, these need to be removed since there aren't string pairs containing "D"'s in the second element of the string pairs in the third and fourth sublists.
I would like for this function to work no matter the number of sublists and no matter the number of elements contained in those sublists. I know how to do this if the list was completely flattened, but it is not in this case. There is no need for this to be an efficient process, so anything that can produce these results would be helpful. Any ideas?
list-manipulation
$endgroup$
add a comment |
$begingroup$
Let's say I have a list containing different sized sublists of string pairs structured like this
"A", "B", "C", "D", "E", "D", "G","B", "H", "B", "I", "B"
where the first and second sublists each contain two string pairs and the third and fourth sublists each contain 1 string pair.
I want to get rid of every string pair where the second string of the string pair does not appear in each sublist. For example, there exists string pairs in each sublist where the second string of the string pair is "B". Thus, I only want to keep those string pairs. The final list should look like this
"A", "B", "G","B", "H", "B", "I", "B"
Although each of the first two sublists contain a pair of strings where there is a "D" in the second element, these need to be removed since there aren't string pairs containing "D"'s in the second element of the string pairs in the third and fourth sublists.
I would like for this function to work no matter the number of sublists and no matter the number of elements contained in those sublists. I know how to do this if the list was completely flattened, but it is not in this case. There is no need for this to be an efficient process, so anything that can produce these results would be helpful. Any ideas?
list-manipulation
$endgroup$
add a comment |
$begingroup$
Let's say I have a list containing different sized sublists of string pairs structured like this
"A", "B", "C", "D", "E", "D", "G","B", "H", "B", "I", "B"
where the first and second sublists each contain two string pairs and the third and fourth sublists each contain 1 string pair.
I want to get rid of every string pair where the second string of the string pair does not appear in each sublist. For example, there exists string pairs in each sublist where the second string of the string pair is "B". Thus, I only want to keep those string pairs. The final list should look like this
"A", "B", "G","B", "H", "B", "I", "B"
Although each of the first two sublists contain a pair of strings where there is a "D" in the second element, these need to be removed since there aren't string pairs containing "D"'s in the second element of the string pairs in the third and fourth sublists.
I would like for this function to work no matter the number of sublists and no matter the number of elements contained in those sublists. I know how to do this if the list was completely flattened, but it is not in this case. There is no need for this to be an efficient process, so anything that can produce these results would be helpful. Any ideas?
list-manipulation
$endgroup$
Let's say I have a list containing different sized sublists of string pairs structured like this
"A", "B", "C", "D", "E", "D", "G","B", "H", "B", "I", "B"
where the first and second sublists each contain two string pairs and the third and fourth sublists each contain 1 string pair.
I want to get rid of every string pair where the second string of the string pair does not appear in each sublist. For example, there exists string pairs in each sublist where the second string of the string pair is "B". Thus, I only want to keep those string pairs. The final list should look like this
"A", "B", "G","B", "H", "B", "I", "B"
Although each of the first two sublists contain a pair of strings where there is a "D" in the second element, these need to be removed since there aren't string pairs containing "D"'s in the second element of the string pairs in the third and fourth sublists.
I would like for this function to work no matter the number of sublists and no matter the number of elements contained in those sublists. I know how to do this if the list was completely flattened, but it is not in this case. There is no need for this to be an efficient process, so anything that can produce these results would be helpful. Any ideas?
list-manipulation
list-manipulation
asked May 11 at 20:59
EricEric
411
411
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
I assume that it's always the second element that determines sublist eligibility:
L = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
Cases[_, Alternatives @@ Intersection @@ L[[All, All, 2]], ___] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
If instead it's always the last element, then the pattern would be
Cases[___, Alternatives @@ Intersection @@ L[[All, All, -1]]] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
add a comment |
$begingroup$
Using GroupBy
and KeyIntersection
:
ClearAll[f1]
f1 = Apply[Join]@*Values@*KeyIntersection@*Map[GroupBy[Last]];
lst = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
f1 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
DeleteCases
seems to be faster:
ClearAll[f2]
f2 = DeleteCases[#, Except@___, Alternatives @@ Intersection @@ #[[All, All, -1]], 2]&;
f2 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
1
$begingroup$
By applying my method atCases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: tryL = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into"A", "B", "X", "B"
instead of keeping its structure as"A", "B", "X", "B"
. Not sure what's the right behavior in this question though.
$endgroup$
– Roman
May 12 at 6:34
1
$begingroup$
@Roman, very good point. I think the result for your example should be"A", "B", "X", "B"
. I removedCases[... ,2]
and replaced it withDeleteCases
.
$endgroup$
– kglr
May 12 at 7:40
add a comment |
$begingroup$
list="A","B","C","D","E","D","G","B","H","B","I","B";
List/@Select[Flatten[list,1],MemberQ[Last@@@Select[list,Length@#==1&],#[[2]]]&]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
add a comment |
$begingroup$
a = "A", "B", "C", "D", "E", "D", "G", "B", "H","B", "I", "B"
f = Select[ContainsAny[Intersection @@ Union @@@ #]@*List@*Last] /@ # &;
f[a]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
add a comment |
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
);
);
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%2f198188%2fextracting-sublists-that-contain-similar-elements%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I assume that it's always the second element that determines sublist eligibility:
L = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
Cases[_, Alternatives @@ Intersection @@ L[[All, All, 2]], ___] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
If instead it's always the last element, then the pattern would be
Cases[___, Alternatives @@ Intersection @@ L[[All, All, -1]]] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
add a comment |
$begingroup$
I assume that it's always the second element that determines sublist eligibility:
L = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
Cases[_, Alternatives @@ Intersection @@ L[[All, All, 2]], ___] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
If instead it's always the last element, then the pattern would be
Cases[___, Alternatives @@ Intersection @@ L[[All, All, -1]]] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
add a comment |
$begingroup$
I assume that it's always the second element that determines sublist eligibility:
L = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
Cases[_, Alternatives @@ Intersection @@ L[[All, All, 2]], ___] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
If instead it's always the last element, then the pattern would be
Cases[___, Alternatives @@ Intersection @@ L[[All, All, -1]]] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
I assume that it's always the second element that determines sublist eligibility:
L = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
Cases[_, Alternatives @@ Intersection @@ L[[All, All, 2]], ___] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
If instead it's always the last element, then the pattern would be
Cases[___, Alternatives @@ Intersection @@ L[[All, All, -1]]] /@ L
"A", "B", "G", "B", "H", "B", "I", "B"
edited May 11 at 21:21
answered May 11 at 21:14
RomanRoman
9,17011440
9,17011440
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
add a comment |
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
$begingroup$
Sorry, I should have been more clear. Both work in my case. Thank you very much!
$endgroup$
– Eric
May 11 at 21:29
add a comment |
$begingroup$
Using GroupBy
and KeyIntersection
:
ClearAll[f1]
f1 = Apply[Join]@*Values@*KeyIntersection@*Map[GroupBy[Last]];
lst = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
f1 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
DeleteCases
seems to be faster:
ClearAll[f2]
f2 = DeleteCases[#, Except@___, Alternatives @@ Intersection @@ #[[All, All, -1]], 2]&;
f2 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
1
$begingroup$
By applying my method atCases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: tryL = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into"A", "B", "X", "B"
instead of keeping its structure as"A", "B", "X", "B"
. Not sure what's the right behavior in this question though.
$endgroup$
– Roman
May 12 at 6:34
1
$begingroup$
@Roman, very good point. I think the result for your example should be"A", "B", "X", "B"
. I removedCases[... ,2]
and replaced it withDeleteCases
.
$endgroup$
– kglr
May 12 at 7:40
add a comment |
$begingroup$
Using GroupBy
and KeyIntersection
:
ClearAll[f1]
f1 = Apply[Join]@*Values@*KeyIntersection@*Map[GroupBy[Last]];
lst = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
f1 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
DeleteCases
seems to be faster:
ClearAll[f2]
f2 = DeleteCases[#, Except@___, Alternatives @@ Intersection @@ #[[All, All, -1]], 2]&;
f2 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
1
$begingroup$
By applying my method atCases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: tryL = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into"A", "B", "X", "B"
instead of keeping its structure as"A", "B", "X", "B"
. Not sure what's the right behavior in this question though.
$endgroup$
– Roman
May 12 at 6:34
1
$begingroup$
@Roman, very good point. I think the result for your example should be"A", "B", "X", "B"
. I removedCases[... ,2]
and replaced it withDeleteCases
.
$endgroup$
– kglr
May 12 at 7:40
add a comment |
$begingroup$
Using GroupBy
and KeyIntersection
:
ClearAll[f1]
f1 = Apply[Join]@*Values@*KeyIntersection@*Map[GroupBy[Last]];
lst = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
f1 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
DeleteCases
seems to be faster:
ClearAll[f2]
f2 = DeleteCases[#, Except@___, Alternatives @@ Intersection @@ #[[All, All, -1]], 2]&;
f2 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
Using GroupBy
and KeyIntersection
:
ClearAll[f1]
f1 = Apply[Join]@*Values@*KeyIntersection@*Map[GroupBy[Last]];
lst = "A", "B", "C", "D", "E", "D", "G", "B", "H", "B", "I", "B";
f1 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
DeleteCases
seems to be faster:
ClearAll[f2]
f2 = DeleteCases[#, Except@___, Alternatives @@ Intersection @@ #[[All, All, -1]], 2]&;
f2 @ lst
"A", "B", "G", "B", "H", "B", "I", "B"
edited May 12 at 7:38
answered May 12 at 4:22
kglrkglr
194k10214435
194k10214435
1
$begingroup$
By applying my method atCases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: tryL = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into"A", "B", "X", "B"
instead of keeping its structure as"A", "B", "X", "B"
. Not sure what's the right behavior in this question though.
$endgroup$
– Roman
May 12 at 6:34
1
$begingroup$
@Roman, very good point. I think the result for your example should be"A", "B", "X", "B"
. I removedCases[... ,2]
and replaced it withDeleteCases
.
$endgroup$
– kglr
May 12 at 7:40
add a comment |
1
$begingroup$
By applying my method atCases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: tryL = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into"A", "B", "X", "B"
instead of keeping its structure as"A", "B", "X", "B"
. Not sure what's the right behavior in this question though.
$endgroup$
– Roman
May 12 at 6:34
1
$begingroup$
@Roman, very good point. I think the result for your example should be"A", "B", "X", "B"
. I removedCases[... ,2]
and replaced it withDeleteCases
.
$endgroup$
– kglr
May 12 at 7:40
1
1
$begingroup$
By applying my method at
Cases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: try L = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into "A", "B", "X", "B"
instead of keeping its structure as "A", "B", "X", "B"
. Not sure what's the right behavior in this question though.$endgroup$
– Roman
May 12 at 6:34
$begingroup$
By applying my method at
Cases
-level 2 and then re-List
ing, you lose information when multiple list members qualify: try L = "A", "B", "C", "D", "X", "B", "E", "D", "G", "B", "H", "B", "I", "B"
, where your method takes the first list apart into "A", "B", "X", "B"
instead of keeping its structure as "A", "B", "X", "B"
. Not sure what's the right behavior in this question though.$endgroup$
– Roman
May 12 at 6:34
1
1
$begingroup$
@Roman, very good point. I think the result for your example should be
"A", "B", "X", "B"
. I removed Cases[... ,2]
and replaced it with DeleteCases
.$endgroup$
– kglr
May 12 at 7:40
$begingroup$
@Roman, very good point. I think the result for your example should be
"A", "B", "X", "B"
. I removed Cases[... ,2]
and replaced it with DeleteCases
.$endgroup$
– kglr
May 12 at 7:40
add a comment |
$begingroup$
list="A","B","C","D","E","D","G","B","H","B","I","B";
List/@Select[Flatten[list,1],MemberQ[Last@@@Select[list,Length@#==1&],#[[2]]]&]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
add a comment |
$begingroup$
list="A","B","C","D","E","D","G","B","H","B","I","B";
List/@Select[Flatten[list,1],MemberQ[Last@@@Select[list,Length@#==1&],#[[2]]]&]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
add a comment |
$begingroup$
list="A","B","C","D","E","D","G","B","H","B","I","B";
List/@Select[Flatten[list,1],MemberQ[Last@@@Select[list,Length@#==1&],#[[2]]]&]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
list="A","B","C","D","E","D","G","B","H","B","I","B";
List/@Select[Flatten[list,1],MemberQ[Last@@@Select[list,Length@#==1&],#[[2]]]&]
"A", "B", "G", "B", "H", "B", "I", "B"
answered May 11 at 21:08
J42161217J42161217
5,335525
5,335525
add a comment |
add a comment |
$begingroup$
a = "A", "B", "C", "D", "E", "D", "G", "B", "H","B", "I", "B"
f = Select[ContainsAny[Intersection @@ Union @@@ #]@*List@*Last] /@ # &;
f[a]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
add a comment |
$begingroup$
a = "A", "B", "C", "D", "E", "D", "G", "B", "H","B", "I", "B"
f = Select[ContainsAny[Intersection @@ Union @@@ #]@*List@*Last] /@ # &;
f[a]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
add a comment |
$begingroup$
a = "A", "B", "C", "D", "E", "D", "G", "B", "H","B", "I", "B"
f = Select[ContainsAny[Intersection @@ Union @@@ #]@*List@*Last] /@ # &;
f[a]
"A", "B", "G", "B", "H", "B", "I", "B"
$endgroup$
a = "A", "B", "C", "D", "E", "D", "G", "B", "H","B", "I", "B"
f = Select[ContainsAny[Intersection @@ Union @@@ #]@*List@*Last] /@ # &;
f[a]
"A", "B", "G", "B", "H", "B", "I", "B"
edited May 11 at 21:14
answered May 11 at 21:08
Henrik SchumacherHenrik Schumacher
63.2k587176
63.2k587176
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%2f198188%2fextracting-sublists-that-contain-similar-elements%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