-1

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 3
    Why do you think it's archaic and unprofessional? Can you elaborate? It looks reasonable to me. – John Kugelman May 20 '22 at 18:56
  • Hi @JohnKugelman I say to be archaic and not professional because it deals with errors instead of a direct hit using some method with ```IF(something...):```, understand? But if this way is the correct model to work with, I will continue to use it. Thanks for support! – Digital Farmer May 20 '22 at 18:59
  • 3
    Does this answer your question? ["Ask forgiveness not permission" - explain](https://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain) Your approach uses this philosophy - try to do the operation anyway, and if it fails then figure out how to handle that. There's nothing wrong with this. The only thing wrong with your code is the bare `except`, which catches all exceptions. You should specify which exception you want to catch, since you know the conversion to float will cause a `ValueError`, you should only catch that – Pranav Hosangadi May 20 '22 at 19:01
  • Use of try/except is entirely correct. I would comment though that deliberately mixing int with float [in the same list] isn't good practice (IMHO). Better to have a 'normal form' which, in this case, should be float. That also makes your code shorter and hence more efficient. I'm also wondering that output is really what you want. If any list contains a value that cannot be converted to either float or int then the entire list is omitted. – DarkKnight May 20 '22 at 19:02
  • Hi @PranavHosangadi Thank you for the link, it was of great value, a lot of important information there! – Digital Farmer May 20 '22 at 19:10
  • Hi @LancelotduLac these values come from an API that I have no control over, so unfortunately I am stuck with this data type until an update is made. But thanks for the tips! – Digital Farmer May 20 '22 at 19:11
  • wouldnt be a possibility to use the opposite approach end filter by `start`, `nan`, ....? just an idea – cards May 20 '22 at 19:42

1 Answers1

1

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)
cards
  • 3,936
  • 1
  • 7
  • 25