python - How to elegantly transform '1-3,6-8' to '1 2 3 6 7 8' within a list? -


problem

background:

i have list of ~10,000 lists containing irregular data needs transformed specific format. data ingested pandas dataframe after transformation.

tl/dr; how elegantly transform matched strings of following regex in list?

regex '\d{1,3}-\d{1,3},\d{1,3}-\d{1,3}'

example: '1-3,6-8' '1 2 3 6 7 8'

current solution: using list comprehensions required multiple type casts transform string , unfit lasting solution.

pat = re.compile('\d{1,3}-\d{1,3},\d{1,3}-\d{1,3}')  row = ['sss-www,ddd-eee', '1-3,6-8', 'xxxx', '0-2,3-7','234','1,5']  lst = [((str(list(range(int(x.split(',')[0].split('-')[0]),      int(x.split(','[0].split('-')[1])+1))).strip('[]').replace(',', '')+' '     +str(list(range(int(x.split(',')[1].split('-')[0]),      int(x.split(',')[1].split('-')[1]) + 1))).strip('[]').replace(',', '')))      if pat.match(str(x)) else x x in row] 

result

    ['sss-www,ddd-eee', '1 2 3 6 7 8', 'xxxx', '0 1 2 3 4 5 6 7', '234', '1,5'] 

capture groups it's easier.

then convert group list integers, , process them 2 2 in list comprehension, chained itertools.chain

import re,itertools  pat = re.compile('(\d{1,3})-(\d{1,3}),(\d{1,3})-(\d{1,3})')  z='1-3,6-8'  groups = [int(x) x in pat.match(z).groups()]  print(list(itertools.chain(*(list(range(groups[i],groups[i+1]+1)) in range(0,len(groups),2))))) 

result:

[1, 2, 3, 6, 7, 8] 

not sure you're calling "elegant", though. remains complicated, because objects return generators need converting list explicitly.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -