regex - Python - re.split: extra empty strings that the beginning and end list -


i'm trying take string of ints and/or floats , create list of floats. string going have these brackets in them need ignored. i'm using re.split, if string begins , ends bracket, empty strings. why that?

code:

import re x = "[1 2 3 4][2 3 4 5]" y =  "1 2 3 4][2 3 4 5" p = re.compile(r'[^\d\.]+') print p.split(x) print p.split(y) 

output:

['', '1', '2', '3', '4', '2', '3', '4', '5', ''] ['1', '2', '3', '4', '2', '3', '4', '5'] 

as more pythonic way can use list comprehension , str.isdigit() method check of character digit :

>>> [i in y if i.isdigit()] ['1', '2', '3', '4', '2', '3', '4', '5'] 

and code first of need split based on space or brackets done [\[\] ] , rid of empty strings leading , trailing brackets can first strip string :

>>> y =  "1 2 3 4][2 3 4 5" >>> re.split(r'[\[\] ]+',y) ['1', '2', '3', '4', '2', '3', '4', '5'] >>> y =  "[1 2 3 4][2 3 4 5]" >>> re.split(r'[\[\] ]+',y) ['', '1', '2', '3', '4', '2', '3', '4', '5', ''] >>> re.split(r'[\[\] ]+',y.strip('[]')) ['1', '2', '3', '4', '2', '3', '4', '5'] 

you can wrap result filter function , using bool function.

>>> filter(bool,re.split(r'[\[\] ]+',y)) ['1', '2', '3', '4', '2', '3', '4', '5'] 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -