How to combine similar characters in a list?












7















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









New contributor




Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    7















    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









    New contributor




    Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      7












      7








      7








      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









      New contributor




      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      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









      New contributor




      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 3 hours ago









      Ajax1234

      41.4k42853




      41.4k42853






      New contributor




      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      GregGreg

      362




      362




      New contributor




      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Greg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes


















          6














          Try:



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





          share|improve this answer



















          • 1





            It gives an empty string at the end.

            – AkshayNevrekar
            3 hours ago






          • 2





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

            – DYZ
            3 hours ago











          • This works for me. Thanks for the help!

            – Greg
            3 hours ago



















          2














          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































            2














            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
              3 hours ago











            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
            });


            }
            });






            Greg is a new contributor. Be nice, and check out our Code of Conduct.










            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









            6














            Try:



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





            share|improve this answer



















            • 1





              It gives an empty string at the end.

              – AkshayNevrekar
              3 hours ago






            • 2





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

              – DYZ
              3 hours ago











            • This works for me. Thanks for the help!

              – Greg
              3 hours ago
















            6














            Try:



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





            share|improve this answer



















            • 1





              It gives an empty string at the end.

              – AkshayNevrekar
              3 hours ago






            • 2





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

              – DYZ
              3 hours ago











            • This works for me. Thanks for the help!

              – Greg
              3 hours ago














            6












            6








            6







            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 3 hours ago









            mingganzmingganz

            30816




            30816








            • 1





              It gives an empty string at the end.

              – AkshayNevrekar
              3 hours ago






            • 2





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

              – DYZ
              3 hours ago











            • This works for me. Thanks for the help!

              – Greg
              3 hours ago














            • 1





              It gives an empty string at the end.

              – AkshayNevrekar
              3 hours ago






            • 2





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

              – DYZ
              3 hours ago











            • This works for me. Thanks for the help!

              – Greg
              3 hours ago








            1




            1





            It gives an empty string at the end.

            – AkshayNevrekar
            3 hours ago





            It gives an empty string at the end.

            – AkshayNevrekar
            3 hours ago




            2




            2





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

            – DYZ
            3 hours ago





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

            – DYZ
            3 hours ago













            This works for me. Thanks for the help!

            – Greg
            3 hours ago





            This works for me. Thanks for the help!

            – Greg
            3 hours ago













            2














            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




























              2














              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


























                2












                2








                2







                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 3 hours ago









                Bodhi94Bodhi94

                948520




                948520























                    2














                    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
                      3 hours ago
















                    2














                    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
                      3 hours ago














                    2












                    2








                    2







                    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 3 hours ago

























                    answered 3 hours ago









                    Ajax1234Ajax1234

                    41.4k42853




                    41.4k42853













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

                      – Greg
                      3 hours ago



















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

                      – Greg
                      3 hours ago

















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

                    – Greg
                    3 hours ago





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

                    – Greg
                    3 hours ago










                    Greg is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Greg is a new contributor. Be nice, and check out our Code of Conduct.













                    Greg is a new contributor. Be nice, and check out our Code of Conduct.












                    Greg is a new contributor. Be nice, and check out our Code of Conduct.
















                    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

                    Ponta tanko

                    Tantalo (mitologio)

                    Erzsébet Schaár