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
- Sử dụng phương thức
7 để kiểm tra xem mỗi ký tự trong chuỗi là một chữ số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[]
- If method return
8, string is a integerdef 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[]
- Nếu phương thức trả về
9, chuỗi là điểm nổidef 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[]
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!
0my_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
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[]
3Nế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
0Copied!
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
Để kiểm tra xem một chuỗi là số nguyên hay phao
- Kết thúc cuộc gọi đến lớp
7 trong khốidef 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[]
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[]
- Nếu cuộc gọi đến lớp
7 thành công, chuỗi là một số nguyêndef 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[]
- If block
0 run, string is floating pointCopied!
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
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[]
0Chúng tôi đã sử dụng một câu lệnh
0 để kiểm tra xem một chuỗi là số nguyên hay phaoCopied!
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
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 integerIf 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ăng4, khốiCopied!
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
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ổiKhô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 thanhUse a tuyên bố Copied!
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"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