Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex String array to float array

Tags:

python

regex

I am a beginner to python regexes. I achieved what I needed but it is really ugly as I am missing experience. My goal is to convert an array of strings of the form:

notes = ["10.0% higher", "5.0% lower", "Same as", "21.2% lower"]

to an array of floats, so that the above array yields:

changes = [10.0,-5.0,0,-21.2]

The code below achieves that but is really repetitive and bad style. How can I optimize that?

changes = []
for note in notes:
    m = re.search(r"(?:(\d+\.\d+\%\shigher)|(\d+\.\d+\%\slower)|(Same\sas))", note)
    if m:
        if m.groups(0):
            if m.groups(0)[0]:
                changes += [float(re.match(r"(\d+\.\d+)", m.groups(0)[0]).groups(0)[0])]
            elif m.groups(0)[1]:
                changes += [-float(re.match(r"(\d+\.\d+)", m.groups(0)[1]).groups(0)[0])]
            else:
                changes += [0.0]
print changes
like image 215
niklas Avatar asked Apr 10 '26 02:04

niklas


1 Answers

Using findall you can do this in a single regex:

notes = ["10.0% higher", "5.0% lower", "Same as", "21.2% lower"]

changes = []
for note in notes:
    m = re.findall("(?:(\d+\.\d+)% )?(higher|lower|Same as)", note)
    if len(m):
       if m[0][1] == 'higher':
          changes += [float(m[0][0])]
       elif m[0][1] == 'lower':
          changes += [-float(m[0][0])]
       else:
          changes += [0.0]

print changes
like image 142
anubhava Avatar answered Apr 12 '26 16:04

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!