Search code examples
pythonfloating-pointintegerlist-comprehension

Separating the lists where after conversion all values are numbers


To separate only the lists that converted become all numbers, I do it like this:

a = ['start','nan','1.004569','0.8554','nan']
b = ['0.464','5','4.8784','2.411474','0.44']
c = ['start','start','start','start','nan']

full_list = [a,b,c]

for test in full_list:
    try:
        numbers = [float(i) if '.' in i or 'e' in i else int(i) for i in test]
        print(numbers)
        print(sum(numbers))
    except:
        pass

output:

[0.464, 5, 4.8784, 2.411474, 0.44]
13.193874000000001

But it seems archaic and unprofessional to use try except in these cases. How should I do this without having to find errors to separate the correct lists and use the numbers later?


Solution

  • Using a regex approach to validate the numerical format. Regex not best but should work for positive and negative integer or decimal and compatible with scientific notation.

    import re
    
    a = ['start','nan','1.004569','0.8554','nan']
    b = ['0.464','-5','4.8784','2.411474','0.44', '-1.23e-07']
    c = ['start','start','start','start','nan']
    
    full_list = [a,b,c]
    
    pattern = re.compile(r'^(-{,1}\d+\.{,1}\d*?)|(-{,1}\d+\.{,1}\d*?e[-\+]\d+)$')
    
    full_results = []
    for test in full_list:
        matches = [pattern.fullmatch(t) for t in test]
        if all(matches):
            results = [float(m.group()) for m in matches]
            full_results.append(results)