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 2 days ago
kglr
159k8184384
159k8184384
asked 2 days ago
Suite401
864212
864212
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
2 days ago
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
2 days ago
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
2 days ago
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 2 days ago
answered 2 days ago
kglr
159k8184384
159k8184384
1
Thank you both!
â Suite401
2 days ago
add a comment |Â
1
Thank you both!
â Suite401
2 days ago
1
1
Thank you both!
â Suite401
2 days ago
Thank you both!
â Suite401
2 days ago
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 2 days ago
m_goldberg
81.8k869190
81.8k869190
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