Deleting specific elements in a list when they come together
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
Suppose I have the following list
l="x","b","c","y","a","d","x","b","y","x","y","c"
I want to go through the list l and delete elements whenever "x"
and "y"
appear both in a single sublist such that I get:
"x","b","c","y","a","d"
Order does not matter, as long as "x"
and "y"
both exist.
list-manipulation
add a comment |Â
up vote
4
down vote
favorite
Suppose I have the following list
l="x","b","c","y","a","d","x","b","y","x","y","c"
I want to go through the list l and delete elements whenever "x"
and "y"
appear both in a single sublist such that I get:
"x","b","c","y","a","d"
Order does not matter, as long as "x"
and "y"
both exist.
list-manipulation
2
Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
â rhermans
Aug 9 at 17:02
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Suppose I have the following list
l="x","b","c","y","a","d","x","b","y","x","y","c"
I want to go through the list l and delete elements whenever "x"
and "y"
appear both in a single sublist such that I get:
"x","b","c","y","a","d"
Order does not matter, as long as "x"
and "y"
both exist.
list-manipulation
Suppose I have the following list
l="x","b","c","y","a","d","x","b","y","x","y","c"
I want to go through the list l and delete elements whenever "x"
and "y"
appear both in a single sublist such that I get:
"x","b","c","y","a","d"
Order does not matter, as long as "x"
and "y"
both exist.
list-manipulation
asked Aug 9 at 16:34
William
35517
35517
2
Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
â rhermans
Aug 9 at 17:02
add a comment |Â
2
Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
â rhermans
Aug 9 at 17:02
2
2
Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
â rhermans
Aug 9 at 17:02
Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
â rhermans
Aug 9 at 17:02
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
Something like the following?
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]
(* Out: âÂÂxâÂÂ, âÂÂbâÂÂ, âÂÂcâÂÂ, âÂÂyâÂÂ, âÂÂaâÂÂ, âÂÂdâ *)
Thank you for this.
â William
Aug 9 at 16:49
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
add a comment |Â
up vote
6
down vote
DeleteCases[l, _?(ContainsAll["x", "y"])]
"x", "b", "c", "y", "a", "d"
add a comment |Â
up vote
5
down vote
A different solution using SubsetQ
and Select
. I also prefer ti limit the scope of the variable definitions using With
or Module
.
With[
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
,
Select[l, Not[SubsetQ[#, "x", "y"]] &]
]
2
It's not documented, butSubsetQ
supports an operator form, so you could eliminate the pure function and instead doSelect[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
1
@CarlWoll Nice tip. I really wish! SubsetQ["x", "y"]
would work however.
â Mr.Wizardâ¦
Aug 9 at 21:44
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Something like the following?
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]
(* Out: âÂÂxâÂÂ, âÂÂbâÂÂ, âÂÂcâÂÂ, âÂÂyâÂÂ, âÂÂaâÂÂ, âÂÂdâ *)
Thank you for this.
â William
Aug 9 at 16:49
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
add a comment |Â
up vote
5
down vote
accepted
Something like the following?
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]
(* Out: âÂÂxâÂÂ, âÂÂbâÂÂ, âÂÂcâÂÂ, âÂÂyâÂÂ, âÂÂaâÂÂ, âÂÂdâ *)
Thank you for this.
â William
Aug 9 at 16:49
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Something like the following?
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]
(* Out: âÂÂxâÂÂ, âÂÂbâÂÂ, âÂÂcâÂÂ, âÂÂyâÂÂ, âÂÂaâÂÂ, âÂÂdâ *)
Something like the following?
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]
(* Out: âÂÂxâÂÂ, âÂÂbâÂÂ, âÂÂcâÂÂ, âÂÂyâÂÂ, âÂÂaâÂÂ, âÂÂdâ *)
edited Aug 9 at 21:43
Mr.Wizardâ¦
227k284631014
227k284631014
answered Aug 9 at 16:47
MarcoB
35.7k556111
35.7k556111
Thank you for this.
â William
Aug 9 at 16:49
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
add a comment |Â
Thank you for this.
â William
Aug 9 at 16:49
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
Thank you for this.
â William
Aug 9 at 16:49
Thank you for this.
â William
Aug 9 at 16:49
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William you are very welcome.
â MarcoB
Aug 9 at 17:20
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
@William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
â user202729
Aug 10 at 3:00
add a comment |Â
up vote
6
down vote
DeleteCases[l, _?(ContainsAll["x", "y"])]
"x", "b", "c", "y", "a", "d"
add a comment |Â
up vote
6
down vote
DeleteCases[l, _?(ContainsAll["x", "y"])]
"x", "b", "c", "y", "a", "d"
add a comment |Â
up vote
6
down vote
up vote
6
down vote
DeleteCases[l, _?(ContainsAll["x", "y"])]
"x", "b", "c", "y", "a", "d"
DeleteCases[l, _?(ContainsAll["x", "y"])]
"x", "b", "c", "y", "a", "d"
answered Aug 9 at 19:36
kglr
157k8182379
157k8182379
add a comment |Â
add a comment |Â
up vote
5
down vote
A different solution using SubsetQ
and Select
. I also prefer ti limit the scope of the variable definitions using With
or Module
.
With[
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
,
Select[l, Not[SubsetQ[#, "x", "y"]] &]
]
2
It's not documented, butSubsetQ
supports an operator form, so you could eliminate the pure function and instead doSelect[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
1
@CarlWoll Nice tip. I really wish! SubsetQ["x", "y"]
would work however.
â Mr.Wizardâ¦
Aug 9 at 21:44
add a comment |Â
up vote
5
down vote
A different solution using SubsetQ
and Select
. I also prefer ti limit the scope of the variable definitions using With
or Module
.
With[
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
,
Select[l, Not[SubsetQ[#, "x", "y"]] &]
]
2
It's not documented, butSubsetQ
supports an operator form, so you could eliminate the pure function and instead doSelect[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
1
@CarlWoll Nice tip. I really wish! SubsetQ["x", "y"]
would work however.
â Mr.Wizardâ¦
Aug 9 at 21:44
add a comment |Â
up vote
5
down vote
up vote
5
down vote
A different solution using SubsetQ
and Select
. I also prefer ti limit the scope of the variable definitions using With
or Module
.
With[
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
,
Select[l, Not[SubsetQ[#, "x", "y"]] &]
]
A different solution using SubsetQ
and Select
. I also prefer ti limit the scope of the variable definitions using With
or Module
.
With[
l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
,
Select[l, Not[SubsetQ[#, "x", "y"]] &]
]
edited Aug 9 at 17:09
answered Aug 9 at 17:01
rhermans
21.6k439103
21.6k439103
2
It's not documented, butSubsetQ
supports an operator form, so you could eliminate the pure function and instead doSelect[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
1
@CarlWoll Nice tip. I really wish! SubsetQ["x", "y"]
would work however.
â Mr.Wizardâ¦
Aug 9 at 21:44
add a comment |Â
2
It's not documented, butSubsetQ
supports an operator form, so you could eliminate the pure function and instead doSelect[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
1
@CarlWoll Nice tip. I really wish! SubsetQ["x", "y"]
would work however.
â Mr.Wizardâ¦
Aug 9 at 21:44
2
2
It's not documented, but
SubsetQ
supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
It's not documented, but
SubsetQ
supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
â Carl Woll
Aug 9 at 18:07
1
1
@CarlWoll Nice tip. I really wish
! SubsetQ["x", "y"]
would work however.â Mr.Wizardâ¦
Aug 9 at 21:44
@CarlWoll Nice tip. I really wish
! SubsetQ["x", "y"]
would work however.â Mr.Wizardâ¦
Aug 9 at 21:44
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f179765%2fdeleting-specific-elements-in-a-list-when-they-come-together%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
â rhermans
Aug 9 at 17:02