1

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.

3 Answers 3

4

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']]
Sign up to request clarification or add additional context in comments.

Comments

0

You could do this with range()

for i in myLst:
  tmp = i.split("-")
  print(list(range(int(tmp[0])+1,int(tmp[1]))))

Comments

0
myLst = ['5-8','20130-23199','1025-2737']

result = []

[result.extend([s.split('-')[0],s.split('-')[1]]) for s in myLst]

print result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.