Reputation: 870
Having a list containing values with special characters in between:
myLst = ['5-8','20130-23199','1025-2737']
How do you extract the values with the -
in between, without using regex?
I "solved" this with regex but it is very slow with huge numbers.
Upvotes: 1
Views: 47
Reputation: 1
myLst = ['5-8','20130-23199','1025-2737']
result = []
[result.extend([s.split('-')[0],s.split('-')[1]]) for s in myLst]
print result
Upvotes: 0
Reputation: 248
You could do this with range()
for i in myLst:
tmp = i.split("-")
print(list(range(int(tmp[0])+1,int(tmp[1]))))
Upvotes: 0
Reputation: 18017
Use str.split
,
myLst = ['5-8','20130-23199','1025-2737']
result = [s.split('-') for s in myLst]
print(result)
#[['5', '8'], ['20130', '23199'], ['1025', '2737']]
Upvotes: 4