问题描述:

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.

网友答案:

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']]
网友答案:

You could do this with range()

for i in myLst:
  tmp = i.split("-")
  print(list(range(int(tmp[0])+1,int(tmp[1]))))
网友答案:
myLst = ['5-8','20130-23199','1025-2737']

result = []

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

print result

相关阅读:
Top