Roy Holzem
Roy Holzem

Reputation: 870

Extracting values from a list without regex

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

Answers (3)

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

result = []

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

print result

Upvotes: 0

Kevin Kendzia
Kevin Kendzia

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

SparkAndShine
SparkAndShine

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

Related Questions