Replace targeted elements in first level of list only
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a list where some elements are strings or sublists:
list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI",
"CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
" D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
" G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
" N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
" L49", " T332", " D362", " G363", " W364", " Q381", " T384",
" Q385", " I388", " N389", " T392", " V395", " N396", " I399",
" E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...
I want to target the 1st level strings that look like "N197"
or "S145"
and replace them with N197
and S145
. Transform from String to List, essentially.
I've tried with
list /.
x_String /;
StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]
This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?
list-manipulation replacement
add a comment |Â
up vote
2
down vote
favorite
I have a list where some elements are strings or sublists:
list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI",
"CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
" D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
" G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
" N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
" L49", " T332", " D362", " G363", " W364", " Q381", " T384",
" Q385", " I388", " N389", " T392", " V395", " N396", " I399",
" E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...
I want to target the 1st level strings that look like "N197"
or "S145"
and replace them with N197
and S145
. Transform from String to List, essentially.
I've tried with
list /.
x_String /;
StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]
This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?
list-manipulation replacement
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a list where some elements are strings or sublists:
list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI",
"CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
" D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
" G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
" N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
" L49", " T332", " D362", " G363", " W364", " Q381", " T384",
" Q385", " I388", " N389", " T392", " V395", " N396", " I399",
" E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...
I want to target the 1st level strings that look like "N197"
or "S145"
and replace them with N197
and S145
. Transform from String to List, essentially.
I've tried with
list /.
x_String /;
StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]
This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?
list-manipulation replacement
I have a list where some elements are strings or sublists:
list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI",
"CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
" D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
" G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
" N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
" L49", " T332", " D362", " G363", " W364", " Q381", " T384",
" Q385", " I388", " N389", " T392", " V395", " N396", " I399",
" E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...
I want to target the 1st level strings that look like "N197"
or "S145"
and replace them with N197
and S145
. Transform from String to List, essentially.
I've tried with
list /.
x_String /;
StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]
This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?
list-manipulation replacement
list-manipulation replacement
edited 12 mins ago
asked 23 mins ago
briennakh
2647
2647
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Do not use ReplaceAll
when you need a level spec. Use Replace
. Avoid in general using ReplaceAll
in favor of Replace
in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll
can bring as you keep building your application and growing your code.
pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];
Replace[list, pattern :> List[x], 1]
Thanks for the tip aboutReplaceAll
!
â briennakh
13 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Do not use ReplaceAll
when you need a level spec. Use Replace
. Avoid in general using ReplaceAll
in favor of Replace
in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll
can bring as you keep building your application and growing your code.
pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];
Replace[list, pattern :> List[x], 1]
Thanks for the tip aboutReplaceAll
!
â briennakh
13 mins ago
add a comment |Â
up vote
3
down vote
accepted
Do not use ReplaceAll
when you need a level spec. Use Replace
. Avoid in general using ReplaceAll
in favor of Replace
in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll
can bring as you keep building your application and growing your code.
pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];
Replace[list, pattern :> List[x], 1]
Thanks for the tip aboutReplaceAll
!
â briennakh
13 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Do not use ReplaceAll
when you need a level spec. Use Replace
. Avoid in general using ReplaceAll
in favor of Replace
in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll
can bring as you keep building your application and growing your code.
pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];
Replace[list, pattern :> List[x], 1]
Do not use ReplaceAll
when you need a level spec. Use Replace
. Avoid in general using ReplaceAll
in favor of Replace
in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll
can bring as you keep building your application and growing your code.
pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];
Replace[list, pattern :> List[x], 1]
answered 16 mins ago
Vitaliy Kaurov
56.2k6157275
56.2k6157275
Thanks for the tip aboutReplaceAll
!
â briennakh
13 mins ago
add a comment |Â
Thanks for the tip aboutReplaceAll
!
â briennakh
13 mins ago
Thanks for the tip about
ReplaceAll
!â briennakh
13 mins ago
Thanks for the tip about
ReplaceAll
!â briennakh
13 mins ago
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%2f183330%2freplace-targeted-elements-in-first-level-of-list-only%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