Nếu bạn quan tâm đến hiệu suất (và tôi không đề xuất bạn nên), cách tiếp cận dựa trên thử là người chiến thắng rõ ràng (so với phương pháp dựa trên phân vùng của bạn hoặc phương pháp RegEXP), miễn là bạn không mong đợi nhiều Các chuỗi không hợp lệ, trong trường hợp đó nó có khả năng chậm hơn (có lẽ là do chi phí xử lý ngoại lệ).
Một lần nữa, tôi không đề nghị bạn quan tâm đến hiệu suất, chỉ cung cấp cho bạn dữ liệu trong trường hợp bạn đang thực hiện việc này 10 tỷ lần một giây hoặc một cái gì đó. Ngoài ra, mã dựa trên phân vùng không xử lý ít nhất một chuỗi hợp lệ.
$ ./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)Đây là mã (Python 2.6, RegEXP lấy từ câu trả lời của John Gietzen):
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()Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác. Bài viết này nhằm mục đích cung cấp thông tin về việc chuyển đổi chuỗi thành float. Trong Python, chúng ta có thể sử dụng float () để chuyển đổi chuỗi thành float. và chúng ta có thể sử dụng int () để chuyển đổi chuỗi thành số nguyên.float() to convert String to float. and we can use int() to convert String to an integer.
Chương trình Python để phân tích một chuỗi vào một chiếc phao
Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số điểm nổi.
Cú pháp của phao:
float(x)Phương thức chỉ chấp nhận một tham số và đó cũng là tùy chọn để sử dụng. Nếu không có đối số được truyền thì phương thức trả về 0,0.
Ví dụ 1: phân tích một chuỗi vào một chiếc phao
Trong ví dụ này, chúng tôi đang chuyển đổi chuỗi thành float.
Python3
string= 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() 0
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() 1def 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() 2
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() 1def 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() 4def 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() 5def 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() 6
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() 7 = 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() 9float(x)0
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() 1def 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() 4def 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() 7float(x)4
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() 1def 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() 4def 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() 5def 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() 4def 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() 73.141 <type 'str'> 3.141 <type 'float'>0
Output:
3.141 <type 'str'> 3.141 <type 'float'>Ví dụ 2: Một chuỗi float số vào phao
Trong ví dụ này, chúng tôi cố gắng chuyển đổi một chuỗi thành một số nguyên và sau đó là một float. Trong đầu ra, chúng ta có thể thấy rằng chúng ta có thể convent một số float chuỗi vào một số nguyên, do đó, chúng ta sẽ gặp lỗi giá trị.
Python3
3.141 <type 'str'> 3.141 <type 'float'>1= 3.141 <type 'str'> 3.141 <type 'float'>3
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() 7 = 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() 9def 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() 43.141 <type 'str'> 3.141 <type 'float'>8def 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() 6
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() 1def 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() 4def 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() 5def 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() 4def 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() 73.141 <type 'str'> 3.141 <type 'float'>0
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() 1def 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() 4ValueError Traceback (most recent call last) <ipython-input-4-8771a1bc4294> in <module>() 1 string = '55.567' 2 ----> 3 Float = float(int(string)) 4 5 print(type(Float)) ValueError: invalid literal for int() with base 10: '55.567'8ValueError Traceback (most recent call last) <ipython-input-4-8771a1bc4294> in <module>() 1 string = '55.567' 2 ----> 3 Float = float(int(string)) 4 5 print(type(Float)) ValueError: invalid literal for int() with base 10: '55.567'9def 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() 7float(x)4
Output:
ValueError Traceback (most recent call last) <ipython-input-4-8771a1bc4294> in <module>() 1 string = '55.567' 2 ----> 3 Float = float(int(string)) 4 5 print(type(Float)) ValueError: invalid literal for int() with base 10: '55.567'Chương trình Python để phân tích một chuỗi vào int
Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số nguyên.
Cú pháp của int
int()Ví dụ 1: phân tích một chuỗi thành một số nguyên
Trong ví dụ này, chúng tôi đang chuyển đổi chuỗi thành số nguyên.
Python3
3.141 <type 'str'> 3.141 <type 'float'>1= int()4
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() 7 = 3.141 <type 'str'> 3.141 <type 'float'>8def 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() 2
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() 1def 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() 4def 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() 5def 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() 4def 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() 73.141 <type 'str'> 3.141 <type 'float'>0
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() 1def 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() 4ValueError Traceback (most recent call last) <ipython-input-4-8771a1bc4294> in <module>() 1 string = '55.567' 2 ----> 3 Float = float(int(string)) 4 5 print(type(Float)) ValueError: invalid literal for int() with base 10: '55.567'8ValueError Traceback (most recent call last) <ipython-input-4-8771a1bc4294> in <module>() 1 string = '55.567' 2 ----> 3 Float = float(int(string)) 4 5 print(type(Float)) ValueError: invalid literal for int() with base 10: '55.567'9__17
Output:
<class 'int'> 55567Ví dụ 2: Một chuỗi nổi số vào một số nguyên
Trong ví dụ này, chúng tôi cố gắng chuyển đổi chuỗi float thành float và sau đó thành số nguyên.
Python3
<class 'int'> 151= <class 'int'> 153
‘
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() 1def 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() 4def 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() 5string3
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() 1string5
Output:
<class 'int'> 15