python通过自定义isnumber函数判断字符串是否为数字的方法

yipeiwu_com6年前Python基础

本文实例讲述了python通过自定义isnumber函数判断字符串是否为数字的方法。分享给大家供大家参考。具体如下:

''' isnumeric.py
test a numeric string s if it's usable
for int(s) or float(s)
'''
def isnumeric(s):
  '''returns True if string s is numeric'''
  return all(c in "0123456789.+-" for c in s)
# test module ...
if __name__ == '__main__':
  print(isnumeric('123'))   # True
  print(isnumeric('-123.45')) # True
  print(isnumeric('+3.14'))  # True
  print(isnumeric('$99.95'))  # False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python计算对角线有理函数插值的方法

本文实例讲述了python计算对角线有理函数插值的方法。分享给大家供大家参考。具体实现方法如下: ''' p = rational(xData,yData,x) Evaluate...

浅谈python日志的配置文件路径问题

如下所示: import logging import logging.config logging.config.fileConfig(path) logger = logging...

Python实现的简单线性回归算法实例分析

本文实例讲述了Python实现的简单线性回归算法。分享给大家供大家参考,具体如下: 用python实现R的线性模型(lm)中一元线性回归的简单方法,使用R的women示例数据,R的运行结...

Python使用re模块实现信息筛选的方法

本文实例讲述了Python使用re模块实现信息筛选的方法。分享给大家供大家参考,具体如下: 背景 平时工作中,我们经常会处理大量的元数据(Raw Data),而一般的文件编辑器只能一次查...

numpy中实现ndarray数组返回符合特定条件的索引方法

numpy中实现ndarray数组返回符合特定条件的索引方法

在numpy的ndarray类型中,似乎没有直接返回特定索引的方法,我只找到了where函数,但是where函数对于寻找某个特定值对应的索引很有用,对于返回一定区间内值的索引不是很有效,...