List rearrangment with joining of strings

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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















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










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"







share|improve this answer



























    up vote
    2
    down vote













    Another one:



     KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]





    share|improve this answer




















    • Thanks Henrik, Kuba; am checking to see which is faster on big data set
      – Suite401
      52 mins ago

















    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.






    share|improve this answer




















    • Wow, Most is a very good idea.
      – Henrik Schumacher
      13 mins ago










    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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"







    share|improve this answer
























      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"







      share|improve this answer






















        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"







        share|improve this answer












        KeyValueMap[
        key, value [Function] Join[key, StringJoin[value]],
        GroupBy[lis, Part[#, 1 ;; 2] & -> Last]
        ]



        "a", "b", "xzw", "d", "e", "y", "d", "c", "x"








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Henrik Schumacher

        39.7k254118




        39.7k254118




















            up vote
            2
            down vote













            Another one:



             KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]





            share|improve this answer




















            • Thanks Henrik, Kuba; am checking to see which is faster on big data set
              – Suite401
              52 mins ago














            up vote
            2
            down vote













            Another one:



             KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]





            share|improve this answer




















            • Thanks Henrik, Kuba; am checking to see which is faster on big data set
              – Suite401
              52 mins ago












            up vote
            2
            down vote










            up vote
            2
            down vote









            Another one:



             KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]





            share|improve this answer












            Another one:



             KeyValueMap[Append] @ Merge[StringJoin][ #, #2 -> #3 & @@@ lis ]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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










            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.






            share|improve this answer




















            • Wow, Most is a very good idea.
              – Henrik Schumacher
              13 mins ago














            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.






            share|improve this answer




















            • Wow, Most is a very good idea.
              – Henrik Schumacher
              13 mins ago












            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 16 mins ago









            Carl Woll

            58.9k276150




            58.9k276150











            • 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




            Wow, Most is a very good idea.
            – Henrik Schumacher
            13 mins ago

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery