Kiểm tra loại chuỗi python

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ử nghiệm là người chiến thắng rõ ràng [vì vậy 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 phí là bạn không

Nội dung chính Hiển thị

  • Kiểm tra xem một chuỗi là số nguyên hay phao trong python #
  • Làm cách nào để kiểm tra xem một chuỗi có nổi không?
  • Số float kiểm tra có phải là chữ số không?
  • Làm thế nào để bạn kiểm tra xem một chuỗi là số thập phân trong Python?
  • Làm thế nào để bạn kiểm tra xem một biến có phải là một float không?

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 công 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]

This is code [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[]

Kiểm tra xem một chuỗi là số nguyên hay phao trong python #

Để kiểm tra xem một chuỗi là số nguyên hay phao

  1. Sử dụng phương thức
    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 để kiểm tra xem mỗi ký tự trong chuỗi là một chữ số
  2. If method return
    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[]
    
    8, string is a integer
  3. Nếu phương thức trả về
    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[]
    
    9, chuỗi là điểm nổi

Copied!

my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468

Nếu bạn phải xử lý các số âm, hãy di chuyển xuống giải pháp

Copied!

my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468
0

Chúng tôi đã sử dụng phương thức

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 để kiểm tra xem tất cả các ký tự trong chuỗi là chữ số

Phương thức str. isdigit trả về

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[]
8 Nếu tất cả các ký tự trong chuỗi là các chữ số và có ít nhất 1 ký tự, nếu không có
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[]
9 được trả về

Phương pháp

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 sẽ trả về
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[]
9 nếu chuỗi có một điểm thập phân hoặc bắt đầu bằng trừ

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[]
4 [is number].
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[]
3

Nếu bạn không phải xử lý các số âm, sử dụng phương pháp

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 là đủ

Nếu bạn phải xử lý các số âm, hãy sử dụng khối

Copied!

my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468
0

Để kiểm tra xem một chuỗi là số nguyên hay phao

  1. Kết thúc cuộc gọi đến lớp
    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 trong khối
    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[]
    
    8
  2. Nếu cuộc gọi đến lớp
    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 thành công, chuỗi là một số nguyên
  3. If block

    Copied!

    my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468
    0 run, string is floating point
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

Chúng tôi đã sử dụng một câu lệnh

Copied!

my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468
0 để kiểm tra xem một chuỗi là số nguyên hay phao

If block

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[]
8 run into public, string is a integer

If call layer

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 với chuỗi sẽ tăng

Copied!

my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468
4, khối
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[]
30 được chạy và chuỗi là số điểm nổi

Không giống như phương pháp

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, phương pháp này cũng xử lý các số âm thanh

Use a tuyên bố

Copied!

my_str = '2468' if my_str.isdigit[]: my_num = int[my_str] print['String is an integer'] else: my_num = float[my_str] print['String is a float'] print[my_num] # 👉️ 2468
0 theo cách này thường được gọi là "yêu cầu tha thứ hơn là sự cho phép"

Chủ Đề