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;
}
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
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.
add a comment |
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
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.
add a comment |
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
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
python python-3.x split
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.
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
If there is always going to be a double dash --
you could do this.
s = '-2--1'
s.replace('--',' -').split(' ') # ['-2', '-1']
add a comment |
import re
s = '-1--2'
result = [int(d) for d in re.findall(r'-?d+', s)]
add a comment |
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
add a comment |
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']
add a comment |
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....
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
If there is always going to be a double dash --
you could do this.
s = '-2--1'
s.replace('--',' -').split(' ') # ['-2', '-1']
add a comment |
If there is always going to be a double dash --
you could do this.
s = '-2--1'
s.replace('--',' -').split(' ') # ['-2', '-1']
add a comment |
If there is always going to be a double dash --
you could do this.
s = '-2--1'
s.replace('--',' -').split(' ') # ['-2', '-1']
If there is always going to be a double dash --
you could do this.
s = '-2--1'
s.replace('--',' -').split(' ') # ['-2', '-1']
answered Nov 17 '18 at 3:08
Red CricketRed Cricket
4,694103390
4,694103390
add a comment |
add a comment |
import re
s = '-1--2'
result = [int(d) for d in re.findall(r'-?d+', s)]
add a comment |
import re
s = '-1--2'
result = [int(d) for d in re.findall(r'-?d+', s)]
add a comment |
import re
s = '-1--2'
result = [int(d) for d in re.findall(r'-?d+', s)]
import re
s = '-1--2'
result = [int(d) for d in re.findall(r'-?d+', s)]
answered Nov 17 '18 at 3:47
YriunsYriuns
278516
278516
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 17 '18 at 3:49
answered Nov 17 '18 at 3:44
cricket_007cricket_007
84.6k1147120
84.6k1147120
add a comment |
add a comment |
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']
add a comment |
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']
add a comment |
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']
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']
answered Nov 17 '18 at 5:50
user2357112user2357112
159k13177272
159k13177272
add a comment |
add a comment |
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....
add a comment |
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....
add a comment |
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....
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....
answered Nov 17 '18 at 5:57
Reut SharabaniReut Sharabani
23.7k44968
23.7k44968
add a comment |
add a comment |