How to combine similar characters in a list?












10















I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:



test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)


Which returns this in the splitter variable:



['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']


I'm trying to combine the hashes so the list turns into this:



['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


Thanks for any help!










share|improve this question

























  • If you want to combine similar characters your result must be ['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.

    – Mykola Zotko
    Feb 9 at 14:26













  • re.split with the right pattern or itertools.groupby in general should do it.

    – pylang
    Feb 11 at 18:42
















10















I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:



test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)


Which returns this in the splitter variable:



['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']


I'm trying to combine the hashes so the list turns into this:



['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


Thanks for any help!










share|improve this question

























  • If you want to combine similar characters your result must be ['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.

    – Mykola Zotko
    Feb 9 at 14:26













  • re.split with the right pattern or itertools.groupby in general should do it.

    – pylang
    Feb 11 at 18:42














10












10








10








I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:



test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)


Which returns this in the splitter variable:



['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']


I'm trying to combine the hashes so the list turns into this:



['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


Thanks for any help!










share|improve this question
















I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:



test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)


Which returns this in the splitter variable:



['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']


I'm trying to combine the hashes so the list turns into this:



['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


Thanks for any help!







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 9 at 4:11









Ajax1234

42.7k42954




42.7k42954










asked Feb 9 at 4:10









GregGreg

543




543













  • If you want to combine similar characters your result must be ['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.

    – Mykola Zotko
    Feb 9 at 14:26













  • re.split with the right pattern or itertools.groupby in general should do it.

    – pylang
    Feb 11 at 18:42



















  • If you want to combine similar characters your result must be ['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.

    – Mykola Zotko
    Feb 9 at 14:26













  • re.split with the right pattern or itertools.groupby in general should do it.

    – pylang
    Feb 11 at 18:42

















If you want to combine similar characters your result must be ['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.

– Mykola Zotko
Feb 9 at 14:26







If you want to combine similar characters your result must be ['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.

– Mykola Zotko
Feb 9 at 14:26















re.split with the right pattern or itertools.groupby in general should do it.

– pylang
Feb 11 at 18:42





re.split with the right pattern or itertools.groupby in general should do it.

– pylang
Feb 11 at 18:42












3 Answers
3






active

oldest

votes


















9














Try:



splitter = re.split("(#+)", test)





share|improve this answer



















  • 1





    It gives an empty string at the end.

    – AkshayNevrekar
    Feb 9 at 4:34






  • 3





    @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

    – DYZ
    Feb 9 at 4:37











  • This works for me. Thanks for the help!

    – Greg
    Feb 9 at 4:43











  • Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

    – pylang
    Feb 11 at 18:39



















6














You can use itertools.groupby:



import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]


Output:



['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


You can also use re.findall:



import re
result = re.findall('#+|[^#]+', test)


Output:



['hello', '###', '_world', '###', 'test', '#', 'test123', '##']





share|improve this answer


























  • This one also does the job. Thanks for the help!

    – Greg
    Feb 9 at 4:44



















3














Add + at the end of the regular expression and filtering None values will do the trick



>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54603094%2fhow-to-combine-similar-characters-in-a-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    Try:



    splitter = re.split("(#+)", test)





    share|improve this answer



















    • 1





      It gives an empty string at the end.

      – AkshayNevrekar
      Feb 9 at 4:34






    • 3





      @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

      – DYZ
      Feb 9 at 4:37











    • This works for me. Thanks for the help!

      – Greg
      Feb 9 at 4:43











    • Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

      – pylang
      Feb 11 at 18:39
















    9














    Try:



    splitter = re.split("(#+)", test)





    share|improve this answer



















    • 1





      It gives an empty string at the end.

      – AkshayNevrekar
      Feb 9 at 4:34






    • 3





      @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

      – DYZ
      Feb 9 at 4:37











    • This works for me. Thanks for the help!

      – Greg
      Feb 9 at 4:43











    • Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

      – pylang
      Feb 11 at 18:39














    9












    9








    9







    Try:



    splitter = re.split("(#+)", test)





    share|improve this answer













    Try:



    splitter = re.split("(#+)", test)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 9 at 4:29









    mingganzmingganz

    33816




    33816








    • 1





      It gives an empty string at the end.

      – AkshayNevrekar
      Feb 9 at 4:34






    • 3





      @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

      – DYZ
      Feb 9 at 4:37











    • This works for me. Thanks for the help!

      – Greg
      Feb 9 at 4:43











    • Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

      – pylang
      Feb 11 at 18:39














    • 1





      It gives an empty string at the end.

      – AkshayNevrekar
      Feb 9 at 4:34






    • 3





      @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

      – DYZ
      Feb 9 at 4:37











    • This works for me. Thanks for the help!

      – Greg
      Feb 9 at 4:43











    • Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

      – pylang
      Feb 11 at 18:39








    1




    1





    It gives an empty string at the end.

    – AkshayNevrekar
    Feb 9 at 4:34





    It gives an empty string at the end.

    – AkshayNevrekar
    Feb 9 at 4:34




    3




    3





    @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

    – DYZ
    Feb 9 at 4:37





    @AkshayNevrekar With splitter = filter(None, splitter) (as in the original post), it does not.

    – DYZ
    Feb 9 at 4:37













    This works for me. Thanks for the help!

    – Greg
    Feb 9 at 4:43





    This works for me. Thanks for the help!

    – Greg
    Feb 9 at 4:43













    Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

    – pylang
    Feb 11 at 18:39





    Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".

    – pylang
    Feb 11 at 18:39













    6














    You can use itertools.groupby:



    import itertools
    test = 'hello###_world###test#test123##'
    new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


    You can also use re.findall:



    import re
    result = re.findall('#+|[^#]+', test)


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']





    share|improve this answer


























    • This one also does the job. Thanks for the help!

      – Greg
      Feb 9 at 4:44
















    6














    You can use itertools.groupby:



    import itertools
    test = 'hello###_world###test#test123##'
    new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


    You can also use re.findall:



    import re
    result = re.findall('#+|[^#]+', test)


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']





    share|improve this answer


























    • This one also does the job. Thanks for the help!

      – Greg
      Feb 9 at 4:44














    6












    6








    6







    You can use itertools.groupby:



    import itertools
    test = 'hello###_world###test#test123##'
    new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


    You can also use re.findall:



    import re
    result = re.findall('#+|[^#]+', test)


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']





    share|improve this answer















    You can use itertools.groupby:



    import itertools
    test = 'hello###_world###test#test123##'
    new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']


    You can also use re.findall:



    import re
    result = re.findall('#+|[^#]+', test)


    Output:



    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 9 at 4:47

























    answered Feb 9 at 4:12









    Ajax1234Ajax1234

    42.7k42954




    42.7k42954













    • This one also does the job. Thanks for the help!

      – Greg
      Feb 9 at 4:44



















    • This one also does the job. Thanks for the help!

      – Greg
      Feb 9 at 4:44

















    This one also does the job. Thanks for the help!

    – Greg
    Feb 9 at 4:44





    This one also does the job. Thanks for the help!

    – Greg
    Feb 9 at 4:44











    3














    Add + at the end of the regular expression and filtering None values will do the trick



    >>> import re
    >>> test = 'hello###_world###test#test123##'
    >>> splitter = re.split("(#+)", test)
    >>> splitter
    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
    >>> splitter = list(filter(None, splitter))
    >>> splitter
    ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
    >>>





    share|improve this answer




























      3














      Add + at the end of the regular expression and filtering None values will do the trick



      >>> import re
      >>> test = 'hello###_world###test#test123##'
      >>> splitter = re.split("(#+)", test)
      >>> splitter
      ['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
      >>> splitter = list(filter(None, splitter))
      >>> splitter
      ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
      >>>





      share|improve this answer


























        3












        3








        3







        Add + at the end of the regular expression and filtering None values will do the trick



        >>> import re
        >>> test = 'hello###_world###test#test123##'
        >>> splitter = re.split("(#+)", test)
        >>> splitter
        ['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
        >>> splitter = list(filter(None, splitter))
        >>> splitter
        ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
        >>>





        share|improve this answer













        Add + at the end of the regular expression and filtering None values will do the trick



        >>> import re
        >>> test = 'hello###_world###test#test123##'
        >>> splitter = re.split("(#+)", test)
        >>> splitter
        ['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
        >>> splitter = list(filter(None, splitter))
        >>> splitter
        ['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
        >>>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 9 at 4:40









        Bodhi94Bodhi94

        987621




        987621






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54603094%2fhow-to-combine-similar-characters-in-a-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Questions related to Moebius Transform of Characteristic Function of the Primes

            List of scandals in India

            Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?