Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you cared about performance (and I'm not suggesting you should), the try-based approach is the clear winner (compared with your partition-based approach or the regexp approach), as long as you don't expect a lot of invalid strings, in which case it's potentially slower (presumably due to the cost of exception handling).</p> <p>Again, I'm not suggesting you care about performance, just giving you the data in case you're doing this 10 billion times a second, or something. Also, the partition-based code doesn't handle at least one valid string.</p> <pre> $ ./floatstr.py F.. partition sad: 3.1102449894 partition happy: 2.09208488464 .. re sad: 7.76906108856 re happy: 7.09421992302 .. try sad: 12.1525540352 try happy: 1.44165301323 . ====================================================================== FAIL: test_partition (__main__.ConvertTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "./floatstr.py", line 48, in test_partition self.failUnless(is_float_partition("20e2")) AssertionError ---------------------------------------------------------------------- Ran 8 tests in 33.670s FAILED (failures=1) </pre> <p>Here's the code (Python 2.6, regexp taken from John Gietzen's <a href="https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python/736117#736117">answer</a>):</p> <pre><code>def is_float_try(str): try: float(str) return True except ValueError: return False import re _float_regexp = re.compile(r"^[-+]?(?:\b[0-9]+(?:\.[0-9]*)?|\.[0-9]+\b)(?:[eE][-+]?[0-9]+\b)?$") def is_float_re(str): return re.match(_float_regexp, str) def is_float_partition(element): partition=element.partition('.') if (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and pa\ rtition[2].isdigit()) or (partition[0].isdigit() and partition[1]=='.' and partition[2]==''): return True if __name__ == '__main__': import unittest import timeit class ConvertTests(unittest.TestCase): def test_re(self): self.failUnless(is_float_re("20e2")) def test_try(self): self.failUnless(is_float_try("20e2")) def test_re_perf(self): print print 're sad:', timeit.Timer('floatstr.is_float_re("12.2x")', "import floatstr").timeit() print 're happy:', timeit.Timer('floatstr.is_float_re("12.2")', "import floatstr").timeit() def test_try_perf(self): print print 'try sad:', timeit.Timer('floatstr.is_float_try("12.2x")', "import floatstr").timeit() print 'try happy:', timeit.Timer('floatstr.is_float_try("12.2")', "import floatstr").timeit() def test_partition_perf(self): print print 'partition sad:', timeit.Timer('floatstr.is_float_partition("12.2x")', "import floatstr").timeit() print 'partition happy:', timeit.Timer('floatstr.is_float_partition("12.2")', "import floatstr").timeit() def test_partition(self): self.failUnless(is_float_partition("20e2")) def test_partition2(self): self.failUnless(is_float_partition(".2")) def test_partition3(self): self.failIf(is_float_partition("1234x.2")) unittest.main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload