Deleting certain integers from string list
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I have a list of strings:
lis = "a","1","b","2","c","3","a","d","4"
and would like to get:
res = "a","b","2","c","3","a","d","4"
where each occurrence of "a"
that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression
followed by IntegerQ
seems inefficient, would be grateful for thoughts.
list-manipulation string-manipulation
add a comment |Â
up vote
8
down vote
favorite
I have a list of strings:
lis = "a","1","b","2","c","3","a","d","4"
and would like to get:
res = "a","b","2","c","3","a","d","4"
where each occurrence of "a"
that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression
followed by IntegerQ
seems inefficient, would be grateful for thoughts.
list-manipulation string-manipulation
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have a list of strings:
lis = "a","1","b","2","c","3","a","d","4"
and would like to get:
res = "a","b","2","c","3","a","d","4"
where each occurrence of "a"
that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression
followed by IntegerQ
seems inefficient, would be grateful for thoughts.
list-manipulation string-manipulation
I have a list of strings:
lis = "a","1","b","2","c","3","a","d","4"
and would like to get:
res = "a","b","2","c","3","a","d","4"
where each occurrence of "a"
that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression
followed by IntegerQ
seems inefficient, would be grateful for thoughts.
list-manipulation string-manipulation
list-manipulation string-manipulation
edited Sep 11 at 4:09
kglr
162k8187386
162k8187386
asked Sep 11 at 3:37
Suite401
903312
903312
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
SequenceReplace
:
SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Also
SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Split
+ ReplaceAll
Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
1
Thank you both!
â Suite401
Sep 11 at 4:19
add a comment |Â
up vote
6
down vote
The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.
lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]
"a", "b", "2", "c", "3", "a", "d", "4"
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
SequenceReplace
:
SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Also
SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Split
+ ReplaceAll
Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
1
Thank you both!
â Suite401
Sep 11 at 4:19
add a comment |Â
up vote
8
down vote
accepted
SequenceReplace
:
SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Also
SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Split
+ ReplaceAll
Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
1
Thank you both!
â Suite401
Sep 11 at 4:19
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
SequenceReplace
:
SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Also
SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Split
+ ReplaceAll
Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
SequenceReplace
:
SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Also
SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
Split
+ ReplaceAll
Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]
ÃÂ "a", "b", "2", "c", "3", "a", "d", "4"
edited Sep 11 at 4:04
answered Sep 11 at 3:51
kglr
162k8187386
162k8187386
1
Thank you both!
â Suite401
Sep 11 at 4:19
add a comment |Â
1
Thank you both!
â Suite401
Sep 11 at 4:19
1
1
Thank you both!
â Suite401
Sep 11 at 4:19
Thank you both!
â Suite401
Sep 11 at 4:19
add a comment |Â
up vote
6
down vote
The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.
lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]
"a", "b", "2", "c", "3", "a", "d", "4"
add a comment |Â
up vote
6
down vote
The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.
lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]
"a", "b", "2", "c", "3", "a", "d", "4"
add a comment |Â
up vote
6
down vote
up vote
6
down vote
The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.
lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]
"a", "b", "2", "c", "3", "a", "d", "4"
The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.
lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]
"a", "b", "2", "c", "3", "a", "d", "4"
answered Sep 11 at 4:02
m_goldberg
82.4k869190
82.4k869190
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%2f181661%2fdeleting-certain-integers-from-string-list%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