Deleting items from a list when they meet certain criteria
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I have a list:
lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"
I would like to obtain:
res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"
where items are dropped from lis
if their middle sub-element is "xx"
and another element in lis
contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]]
puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases
here.
list-manipulation
add a comment |Â
up vote
5
down vote
favorite
I have a list:
lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"
I would like to obtain:
res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"
where items are dropped from lis
if their middle sub-element is "xx"
and another element in lis
contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]]
puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases
here.
list-manipulation
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have a list:
lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"
I would like to obtain:
res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"
where items are dropped from lis
if their middle sub-element is "xx"
and another element in lis
contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]]
puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases
here.
list-manipulation
I have a list:
lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"
I would like to obtain:
res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"
where items are dropped from lis
if their middle sub-element is "xx"
and another element in lis
contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]]
puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases
here.
list-manipulation
list-manipulation
edited Sep 8 at 4:02
m_goldberg
82.3k869190
82.3k869190
asked Sep 8 at 2:55
Suite401
903312
903312
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
Union
:
Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
DeleteCases
:
cond = And[#[[2]] == "xx" ,
MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
DeleteCases[lis, _?cond]
"c", "18", "d", "a", "23", "b", "w", "xx", "z"
A combination of GatherBy
and DeleteCases
:
Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@
GatherBy[lis, #[[1, 3]] &]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
Union
:
Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
DeleteCases
:
cond = And[#[[2]] == "xx" ,
MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
DeleteCases[lis, _?cond]
"c", "18", "d", "a", "23", "b", "w", "xx", "z"
A combination of GatherBy
and DeleteCases
:
Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@
GatherBy[lis, #[[1, 3]] &]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
add a comment |Â
up vote
7
down vote
accepted
Union
:
Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
DeleteCases
:
cond = And[#[[2]] == "xx" ,
MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
DeleteCases[lis, _?cond]
"c", "18", "d", "a", "23", "b", "w", "xx", "z"
A combination of GatherBy
and DeleteCases
:
Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@
GatherBy[lis, #[[1, 3]] &]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
Union
:
Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
DeleteCases
:
cond = And[#[[2]] == "xx" ,
MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
DeleteCases[lis, _?cond]
"c", "18", "d", "a", "23", "b", "w", "xx", "z"
A combination of GatherBy
and DeleteCases
:
Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@
GatherBy[lis, #[[1, 3]] &]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
Union
:
Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
DeleteCases
:
cond = And[#[[2]] == "xx" ,
MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
DeleteCases[lis, _?cond]
"c", "18", "d", "a", "23", "b", "w", "xx", "z"
A combination of GatherBy
and DeleteCases
:
Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@
GatherBy[lis, #[[1, 3]] &]
"a", "23", "b", "c", "18", "d", "w", "xx", "z"
edited Sep 8 at 6:07
answered Sep 8 at 3:31
kglr
162k8187386
162k8187386
add a comment |Â
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%2f181468%2fdeleting-items-from-a-list-when-they-meet-certain-criteria%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