List rearrangment with joining of strings
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a list of strings:
lis = "a","b","x","d","e","y","a","b","z","d","c","x","a","b","w"
I would like to use StringJoin on the last elements where initial elements are the same to obtain:
res = "a","b","xzw","d","e","y","d","c","x"
Doing:
lis = SortBy[lis, #[[1]] &]
SequenceCases[lis, a_, b_, c_, a_, b_, d_ :> a, b, StringJoin[c, d]]
only gives:
"a", "b", "wx"
Thanks for any suggestions.
list-manipulation string-manipulation
add a comment |Â
up vote
3
down vote
favorite
I have a list of strings:
lis = "a","b","x","d","e","y","a","b","z","d","c","x","a","b","w"
I would like to use StringJoin on the last elements where initial elements are the same to obtain:
res = "a","b","xzw","d","e","y","d","c","x"
Doing:
lis = SortBy[lis, #[[1]] &]
SequenceCases[lis, a_, b_, c_, a_, b_, d_ :> a, b, StringJoin[c, d]]
only gives:
"a", "b", "wx"
Thanks for any suggestions.
list-manipulation string-manipulation
At least closely related: mathematica.stackexchange.com/q/26574/5478
â Kubaâ¦
54 mins ago
@Suite401, do you have control on how the "initials" are generated? If they where integers (which can be achieved withToCharacterCode
), then I could also image faster methods.
â Henrik Schumacher
15 mins ago
@Henrik, Each actual element of the list looks like: DateObject,String,String representation of integer,String,String representation of integer,String,String,String so I'm not sure there's much to be done, but am interested in your thoughts.
â Suite401
5 mins ago
Okay, my idea would have required that the number of keys is known a priorily. Btw., I found out that Carl Woll's code is much faster than mine.
â Henrik Schumacher
47 secs ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a list of strings:
lis = "a","b","x","d","e","y","a","b","z","d","c","x","a","b","w"
I would like to use StringJoin on the last elements where initial elements are the same to obtain:
res = "a","b","xzw","d","e","y","d","c","x"
Doing:
lis = SortBy[lis, #[[1]] &]
SequenceCases[lis, a_, b_, c_, a_, b_, d_ :> a, b, StringJoin[c, d]]
only gives:
"a", "b", "wx"
Thanks for any suggestions.
list-manipulation string-manipulation
I have a list of strings:
lis = "a","b","x","d","e","y","a","b","z","d","c","x","a","b","w"
I would like to use StringJoin on the last elements where initial elements are the same to obtain:
res = "a","b","xzw","d","e","y","d","c","x"
Doing:
lis = SortBy[lis, #[[1]] &]
SequenceCases[lis, a_, b_, c_, a_, b_, d_ :> a, b, StringJoin[c, d]]
only gives:
"a", "b", "wx"
Thanks for any suggestions.
list-manipulation string-manipulation
list-manipulation string-manipulation
edited 20 mins ago
Henrik Schumacher
39.7k254118
39.7k254118
asked 1 hour ago
Suite401
898312
898312
At least closely related: mathematica.stackexchange.com/q/26574/5478
â Kubaâ¦
54 mins ago
@Suite401, do you have control on how the "initials" are generated? If they where integers (which can be achieved withToCharacterCode
), then I could also image faster methods.
â Henrik Schumacher
15 mins ago
@Henrik, Each actual element of the list looks like: DateObject,String,String representation of integer,String,String representation of integer,String,String,String so I'm not sure there's much to be done, but am interested in your thoughts.
â Suite401
5 mins ago
Okay, my idea would have required that the number of keys is known a priorily. Btw., I found out that Carl Woll's code is much faster than mine.
â Henrik Schumacher
47 secs ago
add a comment |Â
At least closely related: mathematica.stackexchange.com/q/26574/5478
â Kubaâ¦
54 mins ago
@Suite401, do you have control on how the "initials" are generated? If they where integers (which can be achieved withToCharacterCode
), then I could also image faster methods.
â Henrik Schumacher
15 mins ago
@Henrik, Each actual element of the list looks like: DateObject,String,String representation of integer,String,String representation of integer,String,String,String so I'm not sure there's much to be done, but am interested in your thoughts.
â Suite401
5 mins ago
Okay, my idea would have required that the number of keys is known a priorily. Btw., I found out that Carl Woll's code is much faster than mine.
â Henrik Schumacher
47 secs ago
At least closely related: mathematica.stackexchange.com/q/26574/5478
â Kubaâ¦
54 mins ago
At least closely related: mathematica.stackexchange.com/q/26574/5478
â Kubaâ¦
54 mins ago
@Suite401, do you have control on how the "initials" are generated? If they where integers (which can be achieved with
ToCharacterCode
), then I could also image faster methods.â Henrik Schumacher
15 mins ago
@Suite401, do you have control on how the "initials" are generated? If they where integers (which can be achieved with
ToCharacterCode
), then I could also image faster methods.â Henrik Schumacher
15 mins ago
@Henrik, Each actual element of the list looks like: DateObject,String,String representation of integer,String,String representation of integer,String,String,String so I'm not sure there's much to be done, but am interested in your thoughts.
â Suite401
5 mins ago
@Henrik, Each actual element of the list looks like: DateObject,String,String representation of integer,String,String representation of integer,String,String,String so I'm not sure there's much to be done, but am interested in your thoughts.
â Suite401
5 mins ago
Okay, my idea would have required that the number of keys is known a priorily. Btw., I found out that Carl Woll's code is much faster than mine.
â Henrik Schumacher
47 secs ago
Okay, my idea would have required that the number of keys is known a priorily. Btw., I found out that Carl Woll's code is much faster than mine.
â Henrik Schumacher
47 secs ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
KeyValueMap[
key, value [Function] Join[key, StringJoin[value]],
GroupBy[lis, Part[#, 1 ;; 2] & -> Last]
]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
add a comment |Â
up vote
2
down vote
Another one:
KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
add a comment |Â
up vote
2
down vote
Another variation:
KeyValueMap[Append] @ GroupBy[lis, Most->Last, StringJoin]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
although you may like just the output of GroupBy[lis, Most->Last, StringJoin]
better.
Wow,Most
is a very good idea.
â Henrik Schumacher
13 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
KeyValueMap[
key, value [Function] Join[key, StringJoin[value]],
GroupBy[lis, Part[#, 1 ;; 2] & -> Last]
]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
add a comment |Â
up vote
2
down vote
accepted
KeyValueMap[
key, value [Function] Join[key, StringJoin[value]],
GroupBy[lis, Part[#, 1 ;; 2] & -> Last]
]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
KeyValueMap[
key, value [Function] Join[key, StringJoin[value]],
GroupBy[lis, Part[#, 1 ;; 2] & -> Last]
]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
KeyValueMap[
key, value [Function] Join[key, StringJoin[value]],
GroupBy[lis, Part[#, 1 ;; 2] & -> Last]
]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
answered 1 hour ago
Henrik Schumacher
39.7k254118
39.7k254118
add a comment |Â
add a comment |Â
up vote
2
down vote
Another one:
KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
add a comment |Â
up vote
2
down vote
Another one:
KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Another one:
KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]
Another one:
KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]
answered 56 mins ago
Kubaâ¦
100k11195494
100k11195494
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
add a comment |Â
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
Thanks Henrik, Kuba; am checking to see which is faster on big data set
â Suite401
52 mins ago
add a comment |Â
up vote
2
down vote
Another variation:
KeyValueMap[Append] @ GroupBy[lis, Most->Last, StringJoin]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
although you may like just the output of GroupBy[lis, Most->Last, StringJoin]
better.
Wow,Most
is a very good idea.
â Henrik Schumacher
13 mins ago
add a comment |Â
up vote
2
down vote
Another variation:
KeyValueMap[Append] @ GroupBy[lis, Most->Last, StringJoin]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
although you may like just the output of GroupBy[lis, Most->Last, StringJoin]
better.
Wow,Most
is a very good idea.
â Henrik Schumacher
13 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Another variation:
KeyValueMap[Append] @ GroupBy[lis, Most->Last, StringJoin]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
although you may like just the output of GroupBy[lis, Most->Last, StringJoin]
better.
Another variation:
KeyValueMap[Append] @ GroupBy[lis, Most->Last, StringJoin]
"a", "b", "xzw", "d", "e", "y", "d", "c", "x"
although you may like just the output of GroupBy[lis, Most->Last, StringJoin]
better.
answered 16 mins ago
Carl Woll
58.9k276150
58.9k276150
Wow,Most
is a very good idea.
â Henrik Schumacher
13 mins ago
add a comment |Â
Wow,Most
is a very good idea.
â Henrik Schumacher
13 mins ago
Wow,
Most
is a very good idea.â Henrik Schumacher
13 mins ago
Wow,
Most
is a very good idea.â Henrik Schumacher
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%2f182726%2flist-rearrangment-with-joining-of-strings%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
At least closely related: mathematica.stackexchange.com/q/26574/5478
â Kubaâ¦
54 mins ago
@Suite401, do you have control on how the "initials" are generated? If they where integers (which can be achieved with
ToCharacterCode
), then I could also image faster methods.â Henrik Schumacher
15 mins ago
@Henrik, Each actual element of the list looks like: DateObject,String,String representation of integer,String,String representation of integer,String,String,String so I'm not sure there's much to be done, but am interested in your thoughts.
â Suite401
5 mins ago
Okay, my idea would have required that the number of keys is known a priorily. Btw., I found out that Carl Woll's code is much faster than mine.
â Henrik Schumacher
47 secs ago