We have a large number of strings containing substrings that are possibly integers eg.
mystring = "123 345 456 567 678 789"
and need to verify that:
a. each substring is in fact an integer eg. in mystring = "123 345 456 567 abc 789" fails when it reaches ‘abc’
b. each integer is within the range 0 <= i <= 10000 eg. mystring = "123 -345 456 567 678 789" fails when it reaches ‘-345’
One solution is:
mylist= [int(i) for i in mystring.split() if isinstance(int(i), int) and (0 <= int(i) <= 10000)]
Questions are:
i. In the list comprehension, for each i, does the int(i) get evaluated once or multiple times?
ii. Is there an alternative method that could be faster (as the volume of strings is large and each string could contain hundreds to thousands of integers)?
I think that I would probably use something like:
This has the advantage that it short circuits if something fails to convert to an
intor if it doesn’t fall in the correct range.Here’s a silly test: