检查字符串是否为非负的数字(整数)和字母
您可以使用str.isdigit() https://docs.python.org/2/library/stdtypes.html#str.isdigit and str.isalpha() https://docs.python.org/2/library/stdtypes.html#str.isalpha检查给定的字符串是否是非负的分别为整数(0 或更大)和字母字符。
结果示例:
# For alphabet
>>> 'A'.isdigit()
False
>>> 'A'.isalpha()
True
# For digit
>>> '1'.isdigit()
True
>>> '1'.isalpha()
False
检查字符串是否为正/负 - 整数/浮点数
str.isdigit()
回报False
如果字符串是negative数字或浮点数。例如:
# returns `False` for float
>>> '123.3'.isdigit()
False
# returns `False` for negative number
>>> '-123'.isdigit()
False
如果你想还要检查negative整数和float https://docs.python.org/3/library/functions.html#float,那么您可以编写一个自定义函数来检查它:
def is_number(n):
try:
float(n) # Type-casting the string to `float`.
# If string is not a valid `float`,
# it'll raise `ValueError` exception
except ValueError:
return False
return True
示例运行:
>>> is_number('123') # positive integer number
True
>>> is_number('123.4') # positive float number
True
>>> is_number('-123') # negative integer number
True
>>> is_number('-123.4') # negative `float` number
True
>>> is_number('abc') # `False` for "some random" string
False
检查数字时丢弃“NaN”(不是数字)字符串
上述函数将返回True
对于“NAN”(不是数字)字符串,因为对于 Python 来说,它是表示它不是数字的有效浮点数。例如:
>>> is_number('NaN')
True
为了检查数字是否为“NaN”,您可以使用math.isnan() https://docs.python.org/2/library/math.html#math.isnan as:
>>> import math
>>> nan_num = float('nan')
>>> math.isnan(nan_num)
True
或者,如果您不想导入额外的库来检查这一点,那么您可以简单地通过使用它与自身进行比较来检查它==
。 Python 返回False
when nan
float 与自身进行比较。例如:
# `nan_num` variable is taken from above example
>>> nan_num == nan_num
False
因此,上面功能is_number
可以更新返回False
for "NaN"
as:
def is_number(n):
is_number = True
try:
num = float(n)
# check for "nan" floats
is_number = num == num # or use `math.isnan(num)`
except ValueError:
is_number = False
return is_number
示例运行:
>>> is_number('Nan') # not a number "Nan" string
False
>>> is_number('nan') # not a number string "nan" with all lower cased
False
>>> is_number('123') # positive integer
True
>>> is_number('-123') # negative integer
True
>>> is_number('-1.12') # negative `float`
True
>>> is_number('abc') # "some random" string
False
允许像“1+2j”这样的复数被视为有效数字
上面的函数仍然会返回你False
为了复数 https://docs.python.org/3/library/stdtypes.html#typesnumeric。如果你想要你的is_number
功能治疗复数作为有效号码,那么您需要将传递的字符串类型转换为complex() https://docs.python.org/3/library/functions.html#complex代替float() https://docs.python.org/3/library/functions.html#float。那么你的is_number
函数将如下所示:
def is_number(n):
is_number = True
try:
# v type-casting the number here as `complex`, instead of `float`
num = complex(n)
is_number = num == num
except ValueError:
is_number = False
return is_number
示例运行:
>>> is_number('1+2j') # Valid
True # : complex number
>>> is_number('1+ 2j') # Invalid
False # : string with space in complex number represetantion
# is treated as invalid complex number
>>> is_number('123') # Valid
True # : positive integer
>>> is_number('-123') # Valid
True # : negative integer
>>> is_number('abc') # Invalid
False # : some random string, not a valid number
>>> is_number('nan') # Invalid
False # : not a number "nan" string
PS:根据号码的类型,每次检查的每次操作都会产生额外的开销。选择版本is_number
满足您的要求的功能。