精英盒子 -> 程序设计 -> 怎样判断一个列表中的元素是否为数字? [打印本页]

编程土豆 2012-06-27 18:18

怎样判断一个列表中的元素是否为数字?

RT
问下,Python里怎样判断一个列表中的元素是否为数字,例如有如下代码:
  1. Formula_1 = raw_input()
    Formula_2 = list(Formula_1)


运行如下:
  1. 1+1
    ['1', '+', '1']


怎样判断Formula_2中的第一个元素是否为数字?

jybox 2012-06-27 18:33
isdigit()

目测

编程土豆 2012-06-27 18:37
jybox:
isdigit()
目测

谢谢,确实使用isdigit()
不过貌似不支持负数的检查


jybox 2012-06-27 19:04
编程土豆:谢谢,确实使用isdigit()
不过貌似不支持负数的检查
 (2012-06-27 18:37) 

召唤whtsky吧....

whtsky 2012-06-27 19:35
isdigit是这样的:
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.'

逐个判断string中的字符是否是数字(0-9),但是显然`-`不是数字。。

如果想要加入负数的判断,可以参考这里:https://gist.github.com/3003509

-------------------------------------------------

刚刚被GF拖出去逛了一天街,这几天找我最好短信。。

jybox 2012-06-27 20:31
whtsky:isdigit是这样的:
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.'
逐个判断string中的字符是否是数字(0-9),但是显然`-`不是数字。。
....... (2012-06-27 19:35) 

赤裸裸的炫耀...

scxy 2012-06-27 21:34
Python我还没学那么深..

whtsky 2012-06-27 22:19
jybox:赤裸裸的炫耀... (2012-06-27 20:31) 

这是抱怨。。

jybox 2012-06-27 23:03
whtsky:这是抱怨。。 (2012-06-27 22:19) 

身在福中不知福啊

dditty 2012-07-04 17:33
4楼确实是赤裸裸的炫耀和鄙视啊,话说你电话号码多少,私信我~

whtsky 2012-07-04 19:14
dditty:4楼确实是赤裸裸的炫耀和鄙视啊,话说你电话号码多少,私信我~ (2012-07-04 17:33) 

=__=你要干嘛

jybox 2012-07-04 21:16
whtsky:=__=你要干嘛 (2012-07-04 19:14) 

自然是打击报复




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.044593 second(s),query:5 Gzip enabled