Split '-2--1' into -2 and -1 [closed]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a string which looks like '-2--1' which means from -2 to -1 for my problem. I want to access both of these numbers from the string. str.split("-") won't work in this case. What are my options?



Edit: I can have a string like '2-5' as well, which means from 2 to 5 (In this case I need to extract 2 and 5) or a string like '-2-5' which means -2 to 5(-2 and 5 are the numbers of importance in this case).










share|improve this question















closed as too broad by Red Cricket, petezurich, atline, GhostCat, The fourth bird Nov 17 '18 at 8:57


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

























    0















    I have a string which looks like '-2--1' which means from -2 to -1 for my problem. I want to access both of these numbers from the string. str.split("-") won't work in this case. What are my options?



    Edit: I can have a string like '2-5' as well, which means from 2 to 5 (In this case I need to extract 2 and 5) or a string like '-2-5' which means -2 to 5(-2 and 5 are the numbers of importance in this case).










    share|improve this question















    closed as too broad by Red Cricket, petezurich, atline, GhostCat, The fourth bird Nov 17 '18 at 8:57


    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





















      0












      0








      0








      I have a string which looks like '-2--1' which means from -2 to -1 for my problem. I want to access both of these numbers from the string. str.split("-") won't work in this case. What are my options?



      Edit: I can have a string like '2-5' as well, which means from 2 to 5 (In this case I need to extract 2 and 5) or a string like '-2-5' which means -2 to 5(-2 and 5 are the numbers of importance in this case).










      share|improve this question
















      I have a string which looks like '-2--1' which means from -2 to -1 for my problem. I want to access both of these numbers from the string. str.split("-") won't work in this case. What are my options?



      Edit: I can have a string like '2-5' as well, which means from 2 to 5 (In this case I need to extract 2 and 5) or a string like '-2-5' which means -2 to 5(-2 and 5 are the numbers of importance in this case).







      python python-3.x split






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 6:20









      petezurich

      3,89581936




      3,89581936










      asked Nov 17 '18 at 3:02









      hahajainhahajain

      12




      12




      closed as too broad by Red Cricket, petezurich, atline, GhostCat, The fourth bird Nov 17 '18 at 8:57


      Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









      closed as too broad by Red Cricket, petezurich, atline, GhostCat, The fourth bird Nov 17 '18 at 8:57


      Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


























          5 Answers
          5






          active

          oldest

          votes


















          2














          If there is always going to be a double dash -- you could do this.



          s = '-2--1'
          s.replace('--',' -').split(' ') # ['-2', '-1']





          share|improve this answer































            2














            import re
            s = '-1--2'
            result = [int(d) for d in re.findall(r'-?d+', s)]





            share|improve this answer































              2














              You could use a regex to find all numbers.



              >>> import re 
              >>> re.findall(r'-?d+', '-2--1')
              ['-2', '-1']


              This will work for any characters between numbers. e.g.



              >>> re.findall(r'-?d+', '-2---$&234---1')
              ['-2', '234', '-1']


              But it assumes a - before a number will make it negative, of course






              share|improve this answer

































                1














                Split on the hyphen that comes after a digit:



                def splitrange(s):
                return re.split(r'(?<=d)-', s)


                Demo:



                >>> splitrange('-2--5')
                ['-2', '-5']
                >>> splitrange('-2-5')
                ['-2', '5']
                >>> splitrange('2-5')
                ['2', '5']
                >>> splitrange('2--5')
                ['2', '-5']





                share|improve this answer































                  0














                  split the numbers:



                  split= str.split('-')


                  Function to apply negations:



                  def actual(ns, minus=False):
                  if not ns:
                  return
                  n, *rest = ns
                  if n == '':
                  yield from actual(rest, not minus)
                  return
                  yield -int(n) if minus else int(n)
                  yield from actual(rest)


                  And now you can actualize:



                  numbers = list(actual(split))


                  It will also handle multiple negations....






                  share|improve this answer






























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    2














                    If there is always going to be a double dash -- you could do this.



                    s = '-2--1'
                    s.replace('--',' -').split(' ') # ['-2', '-1']





                    share|improve this answer




























                      2














                      If there is always going to be a double dash -- you could do this.



                      s = '-2--1'
                      s.replace('--',' -').split(' ') # ['-2', '-1']





                      share|improve this answer


























                        2












                        2








                        2







                        If there is always going to be a double dash -- you could do this.



                        s = '-2--1'
                        s.replace('--',' -').split(' ') # ['-2', '-1']





                        share|improve this answer













                        If there is always going to be a double dash -- you could do this.



                        s = '-2--1'
                        s.replace('--',' -').split(' ') # ['-2', '-1']






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 17 '18 at 3:08









                        Red CricketRed Cricket

                        4,694103390




                        4,694103390

























                            2














                            import re
                            s = '-1--2'
                            result = [int(d) for d in re.findall(r'-?d+', s)]





                            share|improve this answer




























                              2














                              import re
                              s = '-1--2'
                              result = [int(d) for d in re.findall(r'-?d+', s)]





                              share|improve this answer


























                                2












                                2








                                2







                                import re
                                s = '-1--2'
                                result = [int(d) for d in re.findall(r'-?d+', s)]





                                share|improve this answer













                                import re
                                s = '-1--2'
                                result = [int(d) for d in re.findall(r'-?d+', s)]






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 17 '18 at 3:47









                                YriunsYriuns

                                278516




                                278516























                                    2














                                    You could use a regex to find all numbers.



                                    >>> import re 
                                    >>> re.findall(r'-?d+', '-2--1')
                                    ['-2', '-1']


                                    This will work for any characters between numbers. e.g.



                                    >>> re.findall(r'-?d+', '-2---$&234---1')
                                    ['-2', '234', '-1']


                                    But it assumes a - before a number will make it negative, of course






                                    share|improve this answer






























                                      2














                                      You could use a regex to find all numbers.



                                      >>> import re 
                                      >>> re.findall(r'-?d+', '-2--1')
                                      ['-2', '-1']


                                      This will work for any characters between numbers. e.g.



                                      >>> re.findall(r'-?d+', '-2---$&234---1')
                                      ['-2', '234', '-1']


                                      But it assumes a - before a number will make it negative, of course






                                      share|improve this answer




























                                        2












                                        2








                                        2







                                        You could use a regex to find all numbers.



                                        >>> import re 
                                        >>> re.findall(r'-?d+', '-2--1')
                                        ['-2', '-1']


                                        This will work for any characters between numbers. e.g.



                                        >>> re.findall(r'-?d+', '-2---$&234---1')
                                        ['-2', '234', '-1']


                                        But it assumes a - before a number will make it negative, of course






                                        share|improve this answer















                                        You could use a regex to find all numbers.



                                        >>> import re 
                                        >>> re.findall(r'-?d+', '-2--1')
                                        ['-2', '-1']


                                        This will work for any characters between numbers. e.g.



                                        >>> re.findall(r'-?d+', '-2---$&234---1')
                                        ['-2', '234', '-1']


                                        But it assumes a - before a number will make it negative, of course







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 17 '18 at 3:49

























                                        answered Nov 17 '18 at 3:44









                                        cricket_007cricket_007

                                        84.6k1147120




                                        84.6k1147120























                                            1














                                            Split on the hyphen that comes after a digit:



                                            def splitrange(s):
                                            return re.split(r'(?<=d)-', s)


                                            Demo:



                                            >>> splitrange('-2--5')
                                            ['-2', '-5']
                                            >>> splitrange('-2-5')
                                            ['-2', '5']
                                            >>> splitrange('2-5')
                                            ['2', '5']
                                            >>> splitrange('2--5')
                                            ['2', '-5']





                                            share|improve this answer




























                                              1














                                              Split on the hyphen that comes after a digit:



                                              def splitrange(s):
                                              return re.split(r'(?<=d)-', s)


                                              Demo:



                                              >>> splitrange('-2--5')
                                              ['-2', '-5']
                                              >>> splitrange('-2-5')
                                              ['-2', '5']
                                              >>> splitrange('2-5')
                                              ['2', '5']
                                              >>> splitrange('2--5')
                                              ['2', '-5']





                                              share|improve this answer


























                                                1












                                                1








                                                1







                                                Split on the hyphen that comes after a digit:



                                                def splitrange(s):
                                                return re.split(r'(?<=d)-', s)


                                                Demo:



                                                >>> splitrange('-2--5')
                                                ['-2', '-5']
                                                >>> splitrange('-2-5')
                                                ['-2', '5']
                                                >>> splitrange('2-5')
                                                ['2', '5']
                                                >>> splitrange('2--5')
                                                ['2', '-5']





                                                share|improve this answer













                                                Split on the hyphen that comes after a digit:



                                                def splitrange(s):
                                                return re.split(r'(?<=d)-', s)


                                                Demo:



                                                >>> splitrange('-2--5')
                                                ['-2', '-5']
                                                >>> splitrange('-2-5')
                                                ['-2', '5']
                                                >>> splitrange('2-5')
                                                ['2', '5']
                                                >>> splitrange('2--5')
                                                ['2', '-5']






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 17 '18 at 5:50









                                                user2357112user2357112

                                                159k13177272




                                                159k13177272























                                                    0














                                                    split the numbers:



                                                    split= str.split('-')


                                                    Function to apply negations:



                                                    def actual(ns, minus=False):
                                                    if not ns:
                                                    return
                                                    n, *rest = ns
                                                    if n == '':
                                                    yield from actual(rest, not minus)
                                                    return
                                                    yield -int(n) if minus else int(n)
                                                    yield from actual(rest)


                                                    And now you can actualize:



                                                    numbers = list(actual(split))


                                                    It will also handle multiple negations....






                                                    share|improve this answer




























                                                      0














                                                      split the numbers:



                                                      split= str.split('-')


                                                      Function to apply negations:



                                                      def actual(ns, minus=False):
                                                      if not ns:
                                                      return
                                                      n, *rest = ns
                                                      if n == '':
                                                      yield from actual(rest, not minus)
                                                      return
                                                      yield -int(n) if minus else int(n)
                                                      yield from actual(rest)


                                                      And now you can actualize:



                                                      numbers = list(actual(split))


                                                      It will also handle multiple negations....






                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        split the numbers:



                                                        split= str.split('-')


                                                        Function to apply negations:



                                                        def actual(ns, minus=False):
                                                        if not ns:
                                                        return
                                                        n, *rest = ns
                                                        if n == '':
                                                        yield from actual(rest, not minus)
                                                        return
                                                        yield -int(n) if minus else int(n)
                                                        yield from actual(rest)


                                                        And now you can actualize:



                                                        numbers = list(actual(split))


                                                        It will also handle multiple negations....






                                                        share|improve this answer













                                                        split the numbers:



                                                        split= str.split('-')


                                                        Function to apply negations:



                                                        def actual(ns, minus=False):
                                                        if not ns:
                                                        return
                                                        n, *rest = ns
                                                        if n == '':
                                                        yield from actual(rest, not minus)
                                                        return
                                                        yield -int(n) if minus else int(n)
                                                        yield from actual(rest)


                                                        And now you can actualize:



                                                        numbers = list(actual(split))


                                                        It will also handle multiple negations....







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 17 '18 at 5:57









                                                        Reut SharabaniReut Sharabani

                                                        23.7k44968




                                                        23.7k44968















                                                            Popular posts from this blog

                                                            Xamarin.iOS Cant Deploy on Iphone

                                                            Glorious Revolution

                                                            Dulmage-Mendelsohn matrix decomposition in Python