What is the best possible way to check if a string can be represented as a number in Python?
检查字符串是否可以表示为Python中的数字的最好方法是什么?
The function I currently have right now is:
我现在拥有的功能是:
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
Which, not only is ugly and slow, seems clunky. However I haven't found a better method because calling float
in the main function is even worse.
不仅丑陋和缓慢,而且看起来很笨拙。但是,我还没有找到更好的方法,因为在main函数中调用float更糟糕。
31 个解决方案
#1
543
Which, not only is ugly and slow
不仅丑陋和缓慢?
I'd dispute both.
我争论。
A regex or other string parsing would be uglier and slower.
regex或其他字符串解析将更加丑陋和缓慢。
I'm not sure that anything much could be faster than the above. It calls the function and returns. Try/Catch doesn't introduce much overhead because the most common exception is caught without an extensive search of stack frames.
我不确定是否有比上述更快的速度。它调用函数和返回。Try/Catch不引入太多的开销,因为最常见的异常是在没有大量搜索堆栈帧的情况下被捕获的。
The issue is that any numeric conversion function has two kinds of results
问题是,任何数字转换函数都有两种结果。
- A number, if the number is valid
- 一个数字,如果这个数字是有效的。
- A status code (e.g., via errno) or exception to show that no valid number could be parsed.
- 状态代码(例如,通过errno)或异常来显示没有有效的数字可以被解析。
C (as an example) hacks around this a number of ways. Python lays it out clearly and explicitly.
C(举个例子)在这方面有很多方法。Python清晰而明确地阐述了这一点。
I think your code for doing this is perfect.
我认为你这样做的代码是完美的。
#2
1280
In case you are looking for parsing (positive, unsigned) integers instead of floats, you can use the isdigit()
function for string objects.
如果您正在寻找解析(正的、无符号的)整数而不是浮点数,您可以使用isdigit()函数来处理字符串对象。
>>> a = "03523"
>>> a.isdigit()
True
>>> b = "963spam"
>>> b.isdigit()
False
字符串方法- isdigit()
There's also something on Unicode strings, which I'm not too familiar with Unicode - Is decimal/decimal
在Unicode字符串中也有一些东西,我不太熟悉Unicode -是十进制/小数。
#3
62
There is one exception that you may want to take into account: the string 'NaN'
有一个例外是你可能想要考虑的:字符串'NaN'
If you want is_number to return FALSE for 'NaN' this code will not work as Python converts it to its representation of a number that is not a number (talk about identity issues):
如果您希望is_number为“NaN”返回FALSE,那么该代码将不能作为Python将其转换为一个数字的表示(讨论身份问题):
>>> float('NaN')
nan
Otherwise, I should actually thank you for the piece of code I now use extensively. :)
否则,我应该感谢您对我现在广泛使用的代码。:)
G.
G。
#4
50
how about this:
这个怎么样:
'3.14'.replace('.','',1).isdigit()
which will return true only if there is one or no '.' in the string of digits.
只有当有一个或不存在时,才会返回真值。”“在那串数字里。
'3.14.5'.replace('.','',1).isdigit()
will return false
将返回错误
edit: just saw another comment ... adding a .replace(badstuff,'',maxnum_badstuff)
for other cases can be done. if you are passing salt and not arbitrary condiments (ref:xkcd#974) this will do fine :P
编辑:刚刚看到另一个评论……添加.replace(badstuff,“maxnum_badstuff”)可以完成其他情况。如果你是在传递盐,而不是任意的调料(ref:xkcd#974),这样做很好:P。
#5
47
TL;DR The best solution is s.replace('.','',1).isdigit()
最好的解决方案是。replace('.', ",1).isdigit()
I did some benchmarks comparing the different approaches
我做了一些基准比较不同的方法。
def is_number_tryexcept(s):
""" Returns True is string is a number. """
try:
float(s)
return True
except ValueError:
return False
import re
def is_number_regex(s):
""" Returns True is string is a number. """
if re.match("^\d+?\.\d+?$", s) is None:
return s.isdigit()
return True
def is_number_repl_isdigit(s):
""" Returns True is string is a number. """
return s.replace('.','',1).isdigit()
If the string is not a number, the except-block is quite slow. But more importantly, the try-except method is the only approach that handles scientific notations correctly.
如果字符串不是一个数字,那么异常块就会很慢。但更重要的是,try-except方法是正确处理科学符号的唯一方法。
funcs = [
is_number_tryexcept,
is_number_regex,
is_number_repl_isdigit
]
a_float = '.1234'
print('Float notation ".1234" is not supported by:')
for f in funcs:
if not f(a_float):
print('\t -', f.__name__)
Float notation ".1234" is not supported by:
- is_number_regex
浮点表示法”。1234不支持:- is_number_regex。
scientific1 = '1.000000e+50'
scientific2 = '1e50'
print('Scientific notation "1.000000e+50" is not supported by:')
for f in funcs:
if not f(scientific1):
print('\t -', f.__name__)
print('Scientific notation "1e50" is not supported by:')
for f in funcs:
if not f(scientific2):
print('\t -', f.__name__)
Scientific notation "1.000000e+50" is not supported by:
- is_number_regex
- is_number_repl_isdigit
Scientific notation "1e50" is not supported by:
- is_number_regex
- is_number_repl_isdigit
科学符号“1.000000e+50”不支持:- is_number_regex - is_number_repl_isdigit科学符号“1e50”不支持:- is_number_regex - is_number_repl_isdigit。
EDIT: The benchmark results
import timeit
test_cases = ['1.12345', '1.12.345', 'abc12345', '12345']
times_n = {f.__name__:[] for f in funcs}
for t in test_cases:
for f in funcs:
f = f.__name__
times_n[f].append(min(timeit.Timer('%s(t)' %f,
'from __main__ import %s, t' %f)
.repeat(repeat=3, number=1000000)))
where the following functions were tested
在哪里测试了以下功能?
from re import match as re_match
from re import compile as re_compile
def is_number_tryexcept(s):
""" Returns True is string is a number. """
try:
float(s)
return True
except ValueError:
return False
def is_number_regex(s):
""" Returns True is string is a number. """
if re_match("^\d+?\.\d+?$", s) is None:
return s.isdigit()
return True
comp = re_compile("^\d+?\.\d+?$")
def compiled_regex(s):
""" Returns True is string is a number. """
if comp.match(s) is None:
return s.isdigit()
return True
def is_number_repl_isdigit(s):
""" Returns True is string is a number. """
return s.replace('.','',1).isdigit()
#6
37
Which, not only is ugly and slow, seems clunky.
不仅丑陋和缓慢,而且看起来很笨拙。
It may take some getting used to, but this is the pythonic way of doing it. As has been already pointed out, the alternatives are worse. But there is one other advantage of doing things this way: polymorphism.
这可能需要一些习惯,但这是python的方法。正如已经指出的那样,替代方案更糟糕。但是这样做还有一个好处:多态性。
The central idea behind duck typing is that "if it walks and talks like a duck, then it's a duck." What if you decide that you need to subclass string so that you can change how you determine if something can be converted into a float? Or what if you decide to test some other object entirely? You can do these things without having to change the above code.
duck typing的中心思想是“如果它像鸭子一样走路和说话,那么它就是鸭子。”如果您决定需要将字符串子类化,以便您可以更改如何确定是否可以将某样东西转换为浮点数,该怎么办?或者,如果你决定完全测试其他对象呢?您可以在不改变上述代码的情况下完成这些工作。
Other languages solve these problems by using interfaces. I'll save the analysis of which solution is better for another thread. The point, though, is that python is decidedly on the duck typing side of the equation, and you're probably going to have to get used to syntax like this if you plan on doing much programming in Python (but that doesn't mean you have to like it of course).
其他语言通过使用接口来解决这些问题。我将保存对另一个线程更好的解决方案的分析。但是,关键是python显然是在等式的鸭子输入端,如果您打算在python中进行大量的编程(但这并不意味着您必须喜欢它),那么您可能不得不习惯这样的语法。
One other thing you might want to take into consideration: Python is pretty fast in throwing and catching exceptions compared to a lot of other languages (30x faster than .Net for instance). Heck, the language itself even throws exceptions to communicate non-exceptional, normal program conditions (every time you use a for loop). Thus, I wouldn't worry too much about the performance aspects of this code until you notice a significant problem.
您还需要考虑的另一件事是:与其他许多语言相比,Python在抛出和捕获异常方面的速度非常快(例如,比. net快30倍)。见鬼,语言本身甚至会抛出异常来传递非异常的、正常的程序条件(每次使用for循环时)。因此,在您注意到一个重要的问题之前,我不会太担心这个代码的性能方面。
#7
37
Updated after Alfe pointed out you don't need to check for float separately as complex handles both:
更新后的Alfe指出,您不需要单独检查浮点数作为复杂句柄:
def is_number(s):
try:
complex(s) # for int, long, float and complex
except ValueError:
return False
return True
Previously said: Is some rare cases you might also need to check for complex numbers (e.g. 1+2i), which can not be represented by a float:
以前说过:有些罕见的情况,你可能还需要检查复数(例如1+2i),不能用浮点数来表示:
def is_number(s):
try:
float(s) # for int, long and float
except ValueError:
try:
complex(s) # for complex
except ValueError:
return False
return True
#8
17
For int
use this:
int使用:
>>> "1221323".isdigit()
True
But for float
we need some tricks ;-). Every float number has one point...
但是对于float,我们需要一些技巧;-)。每个浮点数都有一个点…
>>> "12.34".isdigit()
False
>>> "12.34".replace('.','',1).isdigit()
True
>>> "12.3.4".replace('.','',1).isdigit()
False
Also for negative numbers just add lstrip()
:
对于负数,只需添加lstrip():
>>> '-12'.lstrip('-')
'12'
And now we get a universal way:
现在我们得到了一个普遍的方法:
>>> '-12.34'.lstrip('-').replace('.','',1).isdigit()
True
>>> '.-234'.lstrip('-').replace('.','',1).isdigit()
False
#9
14
Just Mimic C#
In C# there are two different functions that handle parsing of scalar values:
在c#中有两个不同的函数处理标量值的解析:
- Float.Parse()
- Float.Parse()
- Float.TryParse()
- Float.TryParse()
float.parse():
float.parse():
def parse(string):
try:
return float(string)
except Exception:
throw TypeError
Note: If you're wondering why I changed the exception to a TypeError, here's the documentation.
注意:如果您想知道为什么我将异常更改为TypeError,这里是文档。
float.try_parse():
float.try_parse():
def try_parse(string, fail=None):
try:
return float(string)
except Exception:
return fail;
Note: You don't want to return the boolean 'False' because that's still a value type. None is better because it indicates failure. Of course, if you want something different you can change the fail parameter to whatever you want.
注意:您不希望返回布尔“False”,因为它仍然是一个值类型。没有一个更好,因为它表明失败。当然,如果您想要一些不同的东西,您可以将fail参数更改为您想要的任何东西。
To extend float to include the 'parse()' and 'try_parse()' you'll need to monkeypatch the 'float' class to add these methods.
要扩展浮点数,包括“parse()”和“try_parse()”,您需要monkeypatch“float”类来添加这些方法。
If you want respect pre-existing functions the code should be something like:
如果你想要尊重预先存在的函数,代码应该是这样的:
def monkey_patch():
if(!hasattr(float, 'parse')):
float.parse = parse
if(!hasattr(float, 'try_parse')):
float.try_parse = try_parse
SideNote: I personally prefer to call it Monkey Punching because it feels like I'm abusing the language when I do this but YMMV.
SideNote:我个人更喜欢叫它“猴子拳”,因为当我这样做的时候,感觉就像我在滥用语言。
Usage:
用法:
float.parse('giggity') // throws TypeException
float.parse('54.3') // returns the scalar value 54.3
float.tryParse('twank') // returns None
float.tryParse('32.2') // returns the scalar value 32.2
And the great Sage Pythonas said to the Holy See Sharpisus, "Anything you can do I can do better; I can do anything better than you."
大圣人毕达尼亚斯对教廷说:“你能做的任何事我都能做得更好;我可以做任何比你更好的事情。
#10
13
I know this is particularly old but I would add an answer I believe covers the information missing from the highest voted answer that could be very valuable to any who find this:
我知道这一点特别古老,但我会加上一个答案,我相信涵盖了从最高票选答案中遗漏的信息,对任何发现这一点的人来说都是非常宝贵的。
For each of the following methods connect them with a count if you need any input to be accepted. (Assuming we are using vocal definitions of integers rather than 0-255, etc.)
对于下列每一个方法,如果您需要接受任何输入,则将它们与计数联系起来。(假设我们使用的是整数的声音定义,而不是0-255等等)
x.isdigit()
works well for checking if x is an integer.
x.isdigit()可以很好地检查x是否为整数。
x.replace('-','').isdigit()
works well for checking if x is a negative.(Check - in first position)
替换('-',").isdigit()可以很好地检查x是否为负数。(检查-在第一位置)
x.replace('.','').isdigit()
works well for checking if x is a decimal.
替换('.' .).isdigit()可以很好地检查x是否为小数。
x.replace(':','').isdigit()
works well for checking if x is a ratio.
替换(':').isdigit()可以很好地检查x是否是一个比率。
x.replace('/','',1).isdigit()
works well for checking if x is a fraction.
替换('/',",1).isdigit()可以很好地检查x是否为分数。
#11
13
For strings of non-numbers, try: except:
is actually slower than regular expressions. For strings of valid numbers, regex is slower. So, the appropriate method depends on your input.
对于非数字字符串,尝试:除了:实际上比正则表达式慢。对于有效数字字符串,正则表达式比较慢。因此,适当的方法取决于您的输入。
If you find that you are in a performance bind, you can use a new third-party module called fastnumbers that provides a function called isfloat. Full disclosure, I am the author. I have included its results in the timings below.
如果您发现自己处于性能绑定,可以使用一个名为fastnumbers的新第三方模块,该模块提供一个名为isfloat的函数。完全披露,我是作者。我把它的结果包括在下面的时间里。
from __future__ import print_function
import timeit
prep_base = '''\
x = 'invalid'
y = '5402'
z = '4.754e3'
'''
prep_try_method = '''\
def is_number_try(val):
try:
float(val)
return True
except ValueError:
return False
'''
prep_re_method = '''\
import re
float_match = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$').match
def is_number_re(val):
return bool(float_match(val))
'''
fn_method = '''\
from fastnumbers import isfloat
'''
print('Try with non-number strings', timeit.timeit('is_number_try(x)',
prep_base + prep_try_method), 'seconds')
print('Try with integer strings', timeit.timeit('is_number_try(y)',
prep_base + prep_try_method), 'seconds')
print('Try with float strings', timeit.timeit('is_number_try(z)',
prep_base + prep_try_method), 'seconds')
print()
print('Regex with non-number strings', timeit.timeit('is_number_re(x)',
prep_base + prep_re_method), 'seconds')
print('Regex with integer strings', timeit.timeit('is_number_re(y)',
prep_base + prep_re_method), 'seconds')
print('Regex with float strings', timeit.timeit('is_number_re(z)',
prep_base + prep_re_method), 'seconds')
print()
print('fastnumbers with non-number strings', timeit.timeit('isfloat(x)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print('fastnumbers with integer strings', timeit.timeit('isfloat(y)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print('fastnumbers with float strings', timeit.timeit('isfloat(z)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print()
Try with non-number strings 2.39108395576 seconds
Try with integer strings 0.375686168671 seconds
Try with float strings 0.369210958481 seconds
Regex with non-number strings 0.748660802841 seconds
Regex with integer strings 1.02021503448 seconds
Regex with float strings 1.08564686775 seconds
fastnumbers with non-number strings 0.174362897873 seconds
fastnumbers with integer strings 0.179651021957 seconds
fastnumbers with float strings 0.20222902298 seconds
As you can see
正如您可以看到的
-
try: except:
was fast for numeric input but very slow for an invalid input - 尝试:除了:对于数字输入是快速的,但是对于无效的输入很慢。
- regex is very efficient when the input is invalid
- 当输入无效时,正则表达式非常有效。
-
fastnumbers
wins in both cases - 在这两种情况下,快速数字都是双赢的。
#12
10
You can use Unicode strings, they have a method to do just what you want:
你可以使用Unicode字符串,他们有一个方法来做你想做的事情:
>>> s = u"345"
>>> s.isnumeric()
True
Or:
或者:
>>> s = "345"
>>> u = unicode(s)
>>> u.isnumeric()
True
http://www.tutorialspoint.com/python/string_isnumeric.htm
http://www.tutorialspoint.com/python/string_isnumeric.htm
http://docs.python.org/2/howto/unicode.html
http://docs.python.org/2/howto/unicode.html
#13
9
Casting to float and catching ValueError is probably the fastest way, since float() is specifically meant for just that. Anything else that requires string parsing (regex, etc) will likely be slower due to the fact that it's not tuned for this operation. My $0.02.
选择浮动和捕获ValueError可能是最快的方法,因为float()是专门用于这一点的。其他任何需要字符串解析(regex等)的操作都可能会慢一些,因为它不适合这个操作。我的0.02美元。
#14
8
Lets say you have digits in string. str = "100949" and you would like to check if it has only numbers
假设你有字符串中的数字。你想检查一下它是否只有数字。
if str.isdigit():
returns TRUE or FALSE
isdigit文档
otherwise your method works great to find the occurrence of a digit in a string.
否则,您的方法可以很好地找到字符串中位数的出现。
#15
6
So to put it all together, checking for Nan, infinity and complex numbers (it would seem they are specified with j, not i, i.e. 1+2j) it results in:
综上所述,检查Nan,无穷大和复数(似乎它们是用j表示的,而不是i,即1+2j)结果是:
def is_number(s):
try:
n=str(float(s))
if n == "nan" or n=="inf" or n=="-inf" : return False
except ValueError:
try:
complex(s) # for complex
except ValueError:
return False
return True
#16
5
Your code looks fine to me.
你的代码对我来说很好。
Perhaps you think the code is "clunky" because of using exceptions? Note that Python programmers tend to use exceptions liberally when it improves code readability, thanks to its low performance penalty.
也许您认为代码“笨拙”是因为使用了异常?请注意,Python程序员倾向于在提高代码可读性的情况下*使用异常,这要归功于它的低性能惩罚。
#17
5
I wanted to see which method is fastest, and turns out catching an exception is the fastest.
我想知道哪一种方法是最快的,并且发现捕获异常是最快的。
import time
import re
check_regexp = re.compile("^\d*\.?\d*$")
check_replace = lambda x: x.replace('.','',1).isdigit()
numbers = [str(float(x) / 100) for x in xrange(10000000)]
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
start = time.time()
b = [is_number(x) for x in numbers]
print time.time() - start # returns 4.10500001907
start = time.time()
b = [check_regexp.match(x) for x in numbers]
print time.time() - start # returns 5.41799998283
start = time.time()
b = [check_replace(x) for x in numbers]
print time.time() - start # returns 4.5110001564
#18
4
I did some speed test. Lets say that if the string is likely to be a number the try/except strategy is the fastest possible.If the string is not likely to be a number and you are interested in Integer check, it worths to do some test (isdigit plus heading '-'). If you are interested to check float number, you have to use the try/except code whitout escape.
我做了一些速度测试。如果字符串很可能是一个数字,那么尝试/例外策略是最快的。如果字符串不可能是一个数字,并且您对整数检查感兴趣,那么它可以做一些测试(isdigit + heading '-)。如果您有兴趣检查浮点数,您必须使用try/除了代码whitout escape。
#19
3
RyanN suggests
RyanN建议
If you want to return False for a NaN and Inf, change line to x = float(s); return (x == x) and (x - 1 != x). This should return True for all floats except Inf and NaN
如果您想要为NaN和Inf返回False,请将行更改为x = float(s);返回(x == x)和(x - 1 != x)。除了Inf和NaN外,所有浮点数都应该返回True。
But this doesn't quite work, because for sufficiently large floats, x-1 == x
returns true. For example, 2.0**54 - 1 == 2.0**54
但这并不是很有效,因为对于足够大的浮点数,x-1 == x返回true。例如,2.0**54 - 1 == 2.0**54。
#20
3
This answer provides step by step guide having function with examples to find the string is:
这个答案提供了一步一步的向导,用例子来找到这个字符串是:
- Positive integer
- 正整数
- Positive/negative - integer/float
- 正/负-整数/浮动
- How to discard "NaN" (not a number) strings while checking for number?
- 如何在检查数字时丢弃“NaN”(不是数字)字符串?
Check if string is positive integer
You may use str.isdigit()
to check whether given string is positive integer.
您可以使用str.isdigit()来检查给定的字符串是否为正整数。
Sample Results:
结果:样本
# For digit
>>> '1'.isdigit()
True
>>> '1'.isalpha()
False
Check for string as positive/negative - integer/float
str.isdigit()
returns False
if the string is a negative number or a float number. For example:
如果字符串是负数或浮点数,则isdigit()返回False。例如:
# returns `False` for float
>>> '123.3'.isdigit()
False
# returns `False` for negative number
>>> '-123'.isdigit()
False
If you want to also check for the negative integers and float
, then you may write a custom function to check for it as:
如果您还需要检查负整数和浮点数,那么您可以编写一个自定义函数来检查它是否为:
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
Sample Run:
示例运行:
>>> 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
Discard "NaN" (not a number) strings while checking for number
The above functions will return True
for the "NAN" (Not a number) string because for Python it is valid float representing it is not a number. For example:
上面的函数将返回“NAN”(不是一个数字)字符串,因为对于Python来说,它是有效的浮点数,表示它不是一个数字。例如:
>>> is_number('NaN')
True
In order to check whether the number is "NaN", you may use math.isnan()
as:
为了检查数字是否为“NaN”,您可以使用math.isnan()作为:
>>> import math
>>> nan_num = float('nan')
>>> math.isnan(nan_num)
True
Or if you don't want to import additional library to check this, then you may simply check it via comparing it with itself using ==
. Python returns False
when nan
float is compared with itself. For example:
或者,如果您不想导入额外的库来检查这个,那么您可以通过使用==来简单地检查它。当nan float与自身比较时,Python返回False。例如:
# `nan_num` variable is taken from above example
>>> nan_num == nan_num
False
Hence, above function is_number
can be updated to return False
for "NaN"
as:
因此,上面的函数is_number可以被更新为“NaN”返回False:
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
Sample Run:
示例运行:
>>> 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
PS: Each operation for each check depending on the type of number comes with additional overhead. Choose the version of is_number
function which fits your requirement.
每个检查的每个操作取决于数字的类型和额外的开销。选择符合您要求的is_number函数的版本。
#21
2
I needed to determine if a string cast into basic types (float,int,str,bool). After not finding anything on the internet I created this:
我需要确定一个字符串是否被转换为基本类型(float,int,str,bool)。我在网上找不到任何东西,我创造了这个:
def str_to_type (s):
""" Get possible cast type for a string
Parameters
----------
s : string
Returns
-------
float,int,str,bool : type
Depending on what it can be cast to
"""
try:
f = float(s)
if "." not in s:
return int
return float
except ValueError:
value = s.upper()
if value == "TRUE" or value == "FALSE":
return bool
return type(s)
Example
例子
str_to_type("true") # bool
str_to_type("6.0") # float
str_to_type("6") # int
str_to_type("6abc") # str
str_to_type(u"6abc") # unicode
You can capture the type and use it
您可以捕获类型并使用它。
s = "6.0"
type_ = str_to_type(s) # float
f = type_(s)
#22
1
If you want to know if the entire string can be represented as a number you'll want to use a regexp (or maybe convert the float back to a string and compare it to the source string, but I'm guessing that's not very fast).
如果您想知道整个字符串是否可以表示为一个数字,那么您需要使用regexp(或者将浮点数转换为字符串,并将其与源字符串进行比较,但我猜这不是非常快)。
#23
1
Here's my simple way of doing it. Let's say that I'm looping through some strings and I want to add them to an array if they turn out to be numbers.
这是我的简单方法。假设我在某个字符串中循环,如果它们是数字,我想把它们加到数组中。
try:
myvar.append( float(string_to_check) )
except:
continue
Replace the myvar.apppend with whatever operation you want to do with the string if it turns out to be a number. The idea is to try to use a float() operation and use the returned error to determine whether or not the string is a number.
取代myvar一样。如果你想用字符串做任何操作,如果结果是一个数字。其思想是尝试使用float()操作,并使用返回的错误来确定字符串是否是一个数字。
#24
1
Try this.
试试这个。
def is_number(var):
try:
if var == int(var):
return True
except Exception:
return False
#25
1
You may use regex.
你可以使用正则表达式。
number = raw_input("Enter a number: ")
if re.match(r'^\d+$', number):
print "It's integer"
print int(number)
elif re.match(r'^\d+\.\d+$', number):
print "It's float"
print float(number)
else:
print("Please enter a number")
#26
1
I also used the function you mentioned, but soon I notice that strings as "Nan", "Inf" and it's variation are considered as number. So I propose you improved version of your function, that will return false on those type of input and will not fail "1e3" variants:
我还使用了您提到的函数,但很快我注意到字符串是“Nan”,“Inf”,它的变体被认为是数字。因此,我建议你改进你的函数的版本,它将返回错误的输入类型,并且不会失败的“1e3”变体:
def is_float(text):
try:
float(text)
# check for nan/infinity etc.
if text.isalpha():
return False
return True
except ValueError:
return False
#27
0
You can generalize the exception technique in a useful way by returning more useful values than True and False. For example this function puts quotes round strings but leaves numbers alone. Which is just what I needed for a quick and dirty filter to make some variable definitions for R.
您可以通过返回更有用的值而不是True和False来对异常技术进行泛化。例如,这个函数用引号括起来,但只留下数字。这正是我所需要的快速和肮脏的过滤器,为R做一些可变的定义。
import sys
def fix_quotes(s):
try:
float(s)
return s
except ValueError:
return '"{0}"'.format(s)
for line in sys.stdin:
input = line.split()
print input[0], '<- c(', ','.join(fix_quotes(c) for c in input[1:]), ')'
#28
0
I was working on a problem that led me to this thread, namely how to convert a collection of data to strings and numbers in the most intuitive way. I realized after reading the original code that what I needed was different in two ways:
我正在研究一个问题,它将我引向了这个线程,即如何以最直观的方式将数据的集合转换为字符串和数字。在阅读了最初的代码后,我意识到我所需要的是两种不同的方式:
1 - I wanted an integer result if the string represented an integer
1 -如果字符串表示一个整数,我想要一个整数。
2 - I wanted a number or a string result to stick into a data structure
2 -我想要一个数字或一个字符串结果,以插入一个数据结构。
so I adapted the original code to produce this derivative:
所以我修改了原始代码来生成这个导数
def string_or_number(s):
try:
z = int(s)
return z
except ValueError:
try:
z = float(s)
return z
except ValueError:
return s
#29
0
use following it handles all cases:-
使用以下方法处理所有情况:-。
import re
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3sd')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3')
#30
-1
To check if the input value is a float
, you can compare the type of the input to a float
要检查输入值是否为浮点数,您可以将输入的类型与浮动进行比较。
def isFloat(s):
realFloat = 0.1
if type(s) == type(realFloat):
return True
else:
return False
Returns:
返回:
False # s = 5
True # s = 1.2345
The original post would actually return True
for s = 5
since it is a number (integer) and you can cast an int
to a float
without a ValueError
. If you are trying to verify that it is an actual float
instead of just a number, you would need to account for that case.
原来的post实际上会返回s = 5,因为它是一个数字(整数),并且您可以在没有ValueError的情况下将int转换为浮点数。如果您试图验证它是一个实际的浮点数,而不是一个数字,那么您需要考虑这个情况。
#1
543
Which, not only is ugly and slow
不仅丑陋和缓慢?
I'd dispute both.
我争论。
A regex or other string parsing would be uglier and slower.
regex或其他字符串解析将更加丑陋和缓慢。
I'm not sure that anything much could be faster than the above. It calls the function and returns. Try/Catch doesn't introduce much overhead because the most common exception is caught without an extensive search of stack frames.
我不确定是否有比上述更快的速度。它调用函数和返回。Try/Catch不引入太多的开销,因为最常见的异常是在没有大量搜索堆栈帧的情况下被捕获的。
The issue is that any numeric conversion function has two kinds of results
问题是,任何数字转换函数都有两种结果。
- A number, if the number is valid
- 一个数字,如果这个数字是有效的。
- A status code (e.g., via errno) or exception to show that no valid number could be parsed.
- 状态代码(例如,通过errno)或异常来显示没有有效的数字可以被解析。
C (as an example) hacks around this a number of ways. Python lays it out clearly and explicitly.
C(举个例子)在这方面有很多方法。Python清晰而明确地阐述了这一点。
I think your code for doing this is perfect.
我认为你这样做的代码是完美的。
#2
1280
In case you are looking for parsing (positive, unsigned) integers instead of floats, you can use the isdigit()
function for string objects.
如果您正在寻找解析(正的、无符号的)整数而不是浮点数,您可以使用isdigit()函数来处理字符串对象。
>>> a = "03523"
>>> a.isdigit()
True
>>> b = "963spam"
>>> b.isdigit()
False
字符串方法- isdigit()
There's also something on Unicode strings, which I'm not too familiar with Unicode - Is decimal/decimal
在Unicode字符串中也有一些东西,我不太熟悉Unicode -是十进制/小数。
#3
62
There is one exception that you may want to take into account: the string 'NaN'
有一个例外是你可能想要考虑的:字符串'NaN'
If you want is_number to return FALSE for 'NaN' this code will not work as Python converts it to its representation of a number that is not a number (talk about identity issues):
如果您希望is_number为“NaN”返回FALSE,那么该代码将不能作为Python将其转换为一个数字的表示(讨论身份问题):
>>> float('NaN')
nan
Otherwise, I should actually thank you for the piece of code I now use extensively. :)
否则,我应该感谢您对我现在广泛使用的代码。:)
G.
G。
#4
50
how about this:
这个怎么样:
'3.14'.replace('.','',1).isdigit()
which will return true only if there is one or no '.' in the string of digits.
只有当有一个或不存在时,才会返回真值。”“在那串数字里。
'3.14.5'.replace('.','',1).isdigit()
will return false
将返回错误
edit: just saw another comment ... adding a .replace(badstuff,'',maxnum_badstuff)
for other cases can be done. if you are passing salt and not arbitrary condiments (ref:xkcd#974) this will do fine :P
编辑:刚刚看到另一个评论……添加.replace(badstuff,“maxnum_badstuff”)可以完成其他情况。如果你是在传递盐,而不是任意的调料(ref:xkcd#974),这样做很好:P。
#5
47
TL;DR The best solution is s.replace('.','',1).isdigit()
最好的解决方案是。replace('.', ",1).isdigit()
I did some benchmarks comparing the different approaches
我做了一些基准比较不同的方法。
def is_number_tryexcept(s):
""" Returns True is string is a number. """
try:
float(s)
return True
except ValueError:
return False
import re
def is_number_regex(s):
""" Returns True is string is a number. """
if re.match("^\d+?\.\d+?$", s) is None:
return s.isdigit()
return True
def is_number_repl_isdigit(s):
""" Returns True is string is a number. """
return s.replace('.','',1).isdigit()
If the string is not a number, the except-block is quite slow. But more importantly, the try-except method is the only approach that handles scientific notations correctly.
如果字符串不是一个数字,那么异常块就会很慢。但更重要的是,try-except方法是正确处理科学符号的唯一方法。
funcs = [
is_number_tryexcept,
is_number_regex,
is_number_repl_isdigit
]
a_float = '.1234'
print('Float notation ".1234" is not supported by:')
for f in funcs:
if not f(a_float):
print('\t -', f.__name__)
Float notation ".1234" is not supported by:
- is_number_regex
浮点表示法”。1234不支持:- is_number_regex。
scientific1 = '1.000000e+50'
scientific2 = '1e50'
print('Scientific notation "1.000000e+50" is not supported by:')
for f in funcs:
if not f(scientific1):
print('\t -', f.__name__)
print('Scientific notation "1e50" is not supported by:')
for f in funcs:
if not f(scientific2):
print('\t -', f.__name__)
Scientific notation "1.000000e+50" is not supported by:
- is_number_regex
- is_number_repl_isdigit
Scientific notation "1e50" is not supported by:
- is_number_regex
- is_number_repl_isdigit
科学符号“1.000000e+50”不支持:- is_number_regex - is_number_repl_isdigit科学符号“1e50”不支持:- is_number_regex - is_number_repl_isdigit。
EDIT: The benchmark results
import timeit
test_cases = ['1.12345', '1.12.345', 'abc12345', '12345']
times_n = {f.__name__:[] for f in funcs}
for t in test_cases:
for f in funcs:
f = f.__name__
times_n[f].append(min(timeit.Timer('%s(t)' %f,
'from __main__ import %s, t' %f)
.repeat(repeat=3, number=1000000)))
where the following functions were tested
在哪里测试了以下功能?
from re import match as re_match
from re import compile as re_compile
def is_number_tryexcept(s):
""" Returns True is string is a number. """
try:
float(s)
return True
except ValueError:
return False
def is_number_regex(s):
""" Returns True is string is a number. """
if re_match("^\d+?\.\d+?$", s) is None:
return s.isdigit()
return True
comp = re_compile("^\d+?\.\d+?$")
def compiled_regex(s):
""" Returns True is string is a number. """
if comp.match(s) is None:
return s.isdigit()
return True
def is_number_repl_isdigit(s):
""" Returns True is string is a number. """
return s.replace('.','',1).isdigit()
#6
37
Which, not only is ugly and slow, seems clunky.
不仅丑陋和缓慢,而且看起来很笨拙。
It may take some getting used to, but this is the pythonic way of doing it. As has been already pointed out, the alternatives are worse. But there is one other advantage of doing things this way: polymorphism.
这可能需要一些习惯,但这是python的方法。正如已经指出的那样,替代方案更糟糕。但是这样做还有一个好处:多态性。
The central idea behind duck typing is that "if it walks and talks like a duck, then it's a duck." What if you decide that you need to subclass string so that you can change how you determine if something can be converted into a float? Or what if you decide to test some other object entirely? You can do these things without having to change the above code.
duck typing的中心思想是“如果它像鸭子一样走路和说话,那么它就是鸭子。”如果您决定需要将字符串子类化,以便您可以更改如何确定是否可以将某样东西转换为浮点数,该怎么办?或者,如果你决定完全测试其他对象呢?您可以在不改变上述代码的情况下完成这些工作。
Other languages solve these problems by using interfaces. I'll save the analysis of which solution is better for another thread. The point, though, is that python is decidedly on the duck typing side of the equation, and you're probably going to have to get used to syntax like this if you plan on doing much programming in Python (but that doesn't mean you have to like it of course).
其他语言通过使用接口来解决这些问题。我将保存对另一个线程更好的解决方案的分析。但是,关键是python显然是在等式的鸭子输入端,如果您打算在python中进行大量的编程(但这并不意味着您必须喜欢它),那么您可能不得不习惯这样的语法。
One other thing you might want to take into consideration: Python is pretty fast in throwing and catching exceptions compared to a lot of other languages (30x faster than .Net for instance). Heck, the language itself even throws exceptions to communicate non-exceptional, normal program conditions (every time you use a for loop). Thus, I wouldn't worry too much about the performance aspects of this code until you notice a significant problem.
您还需要考虑的另一件事是:与其他许多语言相比,Python在抛出和捕获异常方面的速度非常快(例如,比. net快30倍)。见鬼,语言本身甚至会抛出异常来传递非异常的、正常的程序条件(每次使用for循环时)。因此,在您注意到一个重要的问题之前,我不会太担心这个代码的性能方面。
#7
37
Updated after Alfe pointed out you don't need to check for float separately as complex handles both:
更新后的Alfe指出,您不需要单独检查浮点数作为复杂句柄:
def is_number(s):
try:
complex(s) # for int, long, float and complex
except ValueError:
return False
return True
Previously said: Is some rare cases you might also need to check for complex numbers (e.g. 1+2i), which can not be represented by a float:
以前说过:有些罕见的情况,你可能还需要检查复数(例如1+2i),不能用浮点数来表示:
def is_number(s):
try:
float(s) # for int, long and float
except ValueError:
try:
complex(s) # for complex
except ValueError:
return False
return True
#8
17
For int
use this:
int使用:
>>> "1221323".isdigit()
True
But for float
we need some tricks ;-). Every float number has one point...
但是对于float,我们需要一些技巧;-)。每个浮点数都有一个点…
>>> "12.34".isdigit()
False
>>> "12.34".replace('.','',1).isdigit()
True
>>> "12.3.4".replace('.','',1).isdigit()
False
Also for negative numbers just add lstrip()
:
对于负数,只需添加lstrip():
>>> '-12'.lstrip('-')
'12'
And now we get a universal way:
现在我们得到了一个普遍的方法:
>>> '-12.34'.lstrip('-').replace('.','',1).isdigit()
True
>>> '.-234'.lstrip('-').replace('.','',1).isdigit()
False
#9
14
Just Mimic C#
In C# there are two different functions that handle parsing of scalar values:
在c#中有两个不同的函数处理标量值的解析:
- Float.Parse()
- Float.Parse()
- Float.TryParse()
- Float.TryParse()
float.parse():
float.parse():
def parse(string):
try:
return float(string)
except Exception:
throw TypeError
Note: If you're wondering why I changed the exception to a TypeError, here's the documentation.
注意:如果您想知道为什么我将异常更改为TypeError,这里是文档。
float.try_parse():
float.try_parse():
def try_parse(string, fail=None):
try:
return float(string)
except Exception:
return fail;
Note: You don't want to return the boolean 'False' because that's still a value type. None is better because it indicates failure. Of course, if you want something different you can change the fail parameter to whatever you want.
注意:您不希望返回布尔“False”,因为它仍然是一个值类型。没有一个更好,因为它表明失败。当然,如果您想要一些不同的东西,您可以将fail参数更改为您想要的任何东西。
To extend float to include the 'parse()' and 'try_parse()' you'll need to monkeypatch the 'float' class to add these methods.
要扩展浮点数,包括“parse()”和“try_parse()”,您需要monkeypatch“float”类来添加这些方法。
If you want respect pre-existing functions the code should be something like:
如果你想要尊重预先存在的函数,代码应该是这样的:
def monkey_patch():
if(!hasattr(float, 'parse')):
float.parse = parse
if(!hasattr(float, 'try_parse')):
float.try_parse = try_parse
SideNote: I personally prefer to call it Monkey Punching because it feels like I'm abusing the language when I do this but YMMV.
SideNote:我个人更喜欢叫它“猴子拳”,因为当我这样做的时候,感觉就像我在滥用语言。
Usage:
用法:
float.parse('giggity') // throws TypeException
float.parse('54.3') // returns the scalar value 54.3
float.tryParse('twank') // returns None
float.tryParse('32.2') // returns the scalar value 32.2
And the great Sage Pythonas said to the Holy See Sharpisus, "Anything you can do I can do better; I can do anything better than you."
大圣人毕达尼亚斯对教廷说:“你能做的任何事我都能做得更好;我可以做任何比你更好的事情。
#10
13
I know this is particularly old but I would add an answer I believe covers the information missing from the highest voted answer that could be very valuable to any who find this:
我知道这一点特别古老,但我会加上一个答案,我相信涵盖了从最高票选答案中遗漏的信息,对任何发现这一点的人来说都是非常宝贵的。
For each of the following methods connect them with a count if you need any input to be accepted. (Assuming we are using vocal definitions of integers rather than 0-255, etc.)
对于下列每一个方法,如果您需要接受任何输入,则将它们与计数联系起来。(假设我们使用的是整数的声音定义,而不是0-255等等)
x.isdigit()
works well for checking if x is an integer.
x.isdigit()可以很好地检查x是否为整数。
x.replace('-','').isdigit()
works well for checking if x is a negative.(Check - in first position)
替换('-',").isdigit()可以很好地检查x是否为负数。(检查-在第一位置)
x.replace('.','').isdigit()
works well for checking if x is a decimal.
替换('.' .).isdigit()可以很好地检查x是否为小数。
x.replace(':','').isdigit()
works well for checking if x is a ratio.
替换(':').isdigit()可以很好地检查x是否是一个比率。
x.replace('/','',1).isdigit()
works well for checking if x is a fraction.
替换('/',",1).isdigit()可以很好地检查x是否为分数。
#11
13
For strings of non-numbers, try: except:
is actually slower than regular expressions. For strings of valid numbers, regex is slower. So, the appropriate method depends on your input.
对于非数字字符串,尝试:除了:实际上比正则表达式慢。对于有效数字字符串,正则表达式比较慢。因此,适当的方法取决于您的输入。
If you find that you are in a performance bind, you can use a new third-party module called fastnumbers that provides a function called isfloat. Full disclosure, I am the author. I have included its results in the timings below.
如果您发现自己处于性能绑定,可以使用一个名为fastnumbers的新第三方模块,该模块提供一个名为isfloat的函数。完全披露,我是作者。我把它的结果包括在下面的时间里。
from __future__ import print_function
import timeit
prep_base = '''\
x = 'invalid'
y = '5402'
z = '4.754e3'
'''
prep_try_method = '''\
def is_number_try(val):
try:
float(val)
return True
except ValueError:
return False
'''
prep_re_method = '''\
import re
float_match = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$').match
def is_number_re(val):
return bool(float_match(val))
'''
fn_method = '''\
from fastnumbers import isfloat
'''
print('Try with non-number strings', timeit.timeit('is_number_try(x)',
prep_base + prep_try_method), 'seconds')
print('Try with integer strings', timeit.timeit('is_number_try(y)',
prep_base + prep_try_method), 'seconds')
print('Try with float strings', timeit.timeit('is_number_try(z)',
prep_base + prep_try_method), 'seconds')
print()
print('Regex with non-number strings', timeit.timeit('is_number_re(x)',
prep_base + prep_re_method), 'seconds')
print('Regex with integer strings', timeit.timeit('is_number_re(y)',
prep_base + prep_re_method), 'seconds')
print('Regex with float strings', timeit.timeit('is_number_re(z)',
prep_base + prep_re_method), 'seconds')
print()
print('fastnumbers with non-number strings', timeit.timeit('isfloat(x)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print('fastnumbers with integer strings', timeit.timeit('isfloat(y)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print('fastnumbers with float strings', timeit.timeit('isfloat(z)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print()
Try with non-number strings 2.39108395576 seconds
Try with integer strings 0.375686168671 seconds
Try with float strings 0.369210958481 seconds
Regex with non-number strings 0.748660802841 seconds
Regex with integer strings 1.02021503448 seconds
Regex with float strings 1.08564686775 seconds
fastnumbers with non-number strings 0.174362897873 seconds
fastnumbers with integer strings 0.179651021957 seconds
fastnumbers with float strings 0.20222902298 seconds
As you can see
正如您可以看到的
-
try: except:
was fast for numeric input but very slow for an invalid input - 尝试:除了:对于数字输入是快速的,但是对于无效的输入很慢。
- regex is very efficient when the input is invalid
- 当输入无效时,正则表达式非常有效。
-
fastnumbers
wins in both cases - 在这两种情况下,快速数字都是双赢的。
#12
10
You can use Unicode strings, they have a method to do just what you want:
你可以使用Unicode字符串,他们有一个方法来做你想做的事情:
>>> s = u"345"
>>> s.isnumeric()
True
Or:
或者:
>>> s = "345"
>>> u = unicode(s)
>>> u.isnumeric()
True
http://www.tutorialspoint.com/python/string_isnumeric.htm
http://www.tutorialspoint.com/python/string_isnumeric.htm
http://docs.python.org/2/howto/unicode.html
http://docs.python.org/2/howto/unicode.html
#13
9
Casting to float and catching ValueError is probably the fastest way, since float() is specifically meant for just that. Anything else that requires string parsing (regex, etc) will likely be slower due to the fact that it's not tuned for this operation. My $0.02.
选择浮动和捕获ValueError可能是最快的方法,因为float()是专门用于这一点的。其他任何需要字符串解析(regex等)的操作都可能会慢一些,因为它不适合这个操作。我的0.02美元。
#14
8
Lets say you have digits in string. str = "100949" and you would like to check if it has only numbers
假设你有字符串中的数字。你想检查一下它是否只有数字。
if str.isdigit():
returns TRUE or FALSE
isdigit文档
otherwise your method works great to find the occurrence of a digit in a string.
否则,您的方法可以很好地找到字符串中位数的出现。
#15
6
So to put it all together, checking for Nan, infinity and complex numbers (it would seem they are specified with j, not i, i.e. 1+2j) it results in:
综上所述,检查Nan,无穷大和复数(似乎它们是用j表示的,而不是i,即1+2j)结果是:
def is_number(s):
try:
n=str(float(s))
if n == "nan" or n=="inf" or n=="-inf" : return False
except ValueError:
try:
complex(s) # for complex
except ValueError:
return False
return True
#16
5
Your code looks fine to me.
你的代码对我来说很好。
Perhaps you think the code is "clunky" because of using exceptions? Note that Python programmers tend to use exceptions liberally when it improves code readability, thanks to its low performance penalty.
也许您认为代码“笨拙”是因为使用了异常?请注意,Python程序员倾向于在提高代码可读性的情况下*使用异常,这要归功于它的低性能惩罚。
#17
5
I wanted to see which method is fastest, and turns out catching an exception is the fastest.
我想知道哪一种方法是最快的,并且发现捕获异常是最快的。
import time
import re
check_regexp = re.compile("^\d*\.?\d*$")
check_replace = lambda x: x.replace('.','',1).isdigit()
numbers = [str(float(x) / 100) for x in xrange(10000000)]
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
start = time.time()
b = [is_number(x) for x in numbers]
print time.time() - start # returns 4.10500001907
start = time.time()
b = [check_regexp.match(x) for x in numbers]
print time.time() - start # returns 5.41799998283
start = time.time()
b = [check_replace(x) for x in numbers]
print time.time() - start # returns 4.5110001564
#18
4
I did some speed test. Lets say that if the string is likely to be a number the try/except strategy is the fastest possible.If the string is not likely to be a number and you are interested in Integer check, it worths to do some test (isdigit plus heading '-'). If you are interested to check float number, you have to use the try/except code whitout escape.
我做了一些速度测试。如果字符串很可能是一个数字,那么尝试/例外策略是最快的。如果字符串不可能是一个数字,并且您对整数检查感兴趣,那么它可以做一些测试(isdigit + heading '-)。如果您有兴趣检查浮点数,您必须使用try/除了代码whitout escape。
#19
3
RyanN suggests
RyanN建议
If you want to return False for a NaN and Inf, change line to x = float(s); return (x == x) and (x - 1 != x). This should return True for all floats except Inf and NaN
如果您想要为NaN和Inf返回False,请将行更改为x = float(s);返回(x == x)和(x - 1 != x)。除了Inf和NaN外,所有浮点数都应该返回True。
But this doesn't quite work, because for sufficiently large floats, x-1 == x
returns true. For example, 2.0**54 - 1 == 2.0**54
但这并不是很有效,因为对于足够大的浮点数,x-1 == x返回true。例如,2.0**54 - 1 == 2.0**54。
#20
3
This answer provides step by step guide having function with examples to find the string is:
这个答案提供了一步一步的向导,用例子来找到这个字符串是:
- Positive integer
- 正整数
- Positive/negative - integer/float
- 正/负-整数/浮动
- How to discard "NaN" (not a number) strings while checking for number?
- 如何在检查数字时丢弃“NaN”(不是数字)字符串?
Check if string is positive integer
You may use str.isdigit()
to check whether given string is positive integer.
您可以使用str.isdigit()来检查给定的字符串是否为正整数。
Sample Results:
结果:样本
# For digit
>>> '1'.isdigit()
True
>>> '1'.isalpha()
False
Check for string as positive/negative - integer/float
str.isdigit()
returns False
if the string is a negative number or a float number. For example:
如果字符串是负数或浮点数,则isdigit()返回False。例如:
# returns `False` for float
>>> '123.3'.isdigit()
False
# returns `False` for negative number
>>> '-123'.isdigit()
False
If you want to also check for the negative integers and float
, then you may write a custom function to check for it as:
如果您还需要检查负整数和浮点数,那么您可以编写一个自定义函数来检查它是否为:
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
Sample Run:
示例运行:
>>> 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
Discard "NaN" (not a number) strings while checking for number
The above functions will return True
for the "NAN" (Not a number) string because for Python it is valid float representing it is not a number. For example:
上面的函数将返回“NAN”(不是一个数字)字符串,因为对于Python来说,它是有效的浮点数,表示它不是一个数字。例如:
>>> is_number('NaN')
True
In order to check whether the number is "NaN", you may use math.isnan()
as:
为了检查数字是否为“NaN”,您可以使用math.isnan()作为:
>>> import math
>>> nan_num = float('nan')
>>> math.isnan(nan_num)
True
Or if you don't want to import additional library to check this, then you may simply check it via comparing it with itself using ==
. Python returns False
when nan
float is compared with itself. For example:
或者,如果您不想导入额外的库来检查这个,那么您可以通过使用==来简单地检查它。当nan float与自身比较时,Python返回False。例如:
# `nan_num` variable is taken from above example
>>> nan_num == nan_num
False
Hence, above function is_number
can be updated to return False
for "NaN"
as:
因此,上面的函数is_number可以被更新为“NaN”返回False:
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
Sample Run:
示例运行:
>>> 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
PS: Each operation for each check depending on the type of number comes with additional overhead. Choose the version of is_number
function which fits your requirement.
每个检查的每个操作取决于数字的类型和额外的开销。选择符合您要求的is_number函数的版本。
#21
2
I needed to determine if a string cast into basic types (float,int,str,bool). After not finding anything on the internet I created this:
我需要确定一个字符串是否被转换为基本类型(float,int,str,bool)。我在网上找不到任何东西,我创造了这个:
def str_to_type (s):
""" Get possible cast type for a string
Parameters
----------
s : string
Returns
-------
float,int,str,bool : type
Depending on what it can be cast to
"""
try:
f = float(s)
if "." not in s:
return int
return float
except ValueError:
value = s.upper()
if value == "TRUE" or value == "FALSE":
return bool
return type(s)
Example
例子
str_to_type("true") # bool
str_to_type("6.0") # float
str_to_type("6") # int
str_to_type("6abc") # str
str_to_type(u"6abc") # unicode
You can capture the type and use it
您可以捕获类型并使用它。
s = "6.0"
type_ = str_to_type(s) # float
f = type_(s)
#22
1
If you want to know if the entire string can be represented as a number you'll want to use a regexp (or maybe convert the float back to a string and compare it to the source string, but I'm guessing that's not very fast).
如果您想知道整个字符串是否可以表示为一个数字,那么您需要使用regexp(或者将浮点数转换为字符串,并将其与源字符串进行比较,但我猜这不是非常快)。
#23
1
Here's my simple way of doing it. Let's say that I'm looping through some strings and I want to add them to an array if they turn out to be numbers.
这是我的简单方法。假设我在某个字符串中循环,如果它们是数字,我想把它们加到数组中。
try:
myvar.append( float(string_to_check) )
except:
continue
Replace the myvar.apppend with whatever operation you want to do with the string if it turns out to be a number. The idea is to try to use a float() operation and use the returned error to determine whether or not the string is a number.
取代myvar一样。如果你想用字符串做任何操作,如果结果是一个数字。其思想是尝试使用float()操作,并使用返回的错误来确定字符串是否是一个数字。
#24
1
Try this.
试试这个。
def is_number(var):
try:
if var == int(var):
return True
except Exception:
return False
#25
1
You may use regex.
你可以使用正则表达式。
number = raw_input("Enter a number: ")
if re.match(r'^\d+$', number):
print "It's integer"
print int(number)
elif re.match(r'^\d+\.\d+$', number):
print "It's float"
print float(number)
else:
print("Please enter a number")
#26
1
I also used the function you mentioned, but soon I notice that strings as "Nan", "Inf" and it's variation are considered as number. So I propose you improved version of your function, that will return false on those type of input and will not fail "1e3" variants:
我还使用了您提到的函数,但很快我注意到字符串是“Nan”,“Inf”,它的变体被认为是数字。因此,我建议你改进你的函数的版本,它将返回错误的输入类型,并且不会失败的“1e3”变体:
def is_float(text):
try:
float(text)
# check for nan/infinity etc.
if text.isalpha():
return False
return True
except ValueError:
return False
#27
0
You can generalize the exception technique in a useful way by returning more useful values than True and False. For example this function puts quotes round strings but leaves numbers alone. Which is just what I needed for a quick and dirty filter to make some variable definitions for R.
您可以通过返回更有用的值而不是True和False来对异常技术进行泛化。例如,这个函数用引号括起来,但只留下数字。这正是我所需要的快速和肮脏的过滤器,为R做一些可变的定义。
import sys
def fix_quotes(s):
try:
float(s)
return s
except ValueError:
return '"{0}"'.format(s)
for line in sys.stdin:
input = line.split()
print input[0], '<- c(', ','.join(fix_quotes(c) for c in input[1:]), ')'
#28
0
I was working on a problem that led me to this thread, namely how to convert a collection of data to strings and numbers in the most intuitive way. I realized after reading the original code that what I needed was different in two ways:
我正在研究一个问题,它将我引向了这个线程,即如何以最直观的方式将数据的集合转换为字符串和数字。在阅读了最初的代码后,我意识到我所需要的是两种不同的方式:
1 - I wanted an integer result if the string represented an integer
1 -如果字符串表示一个整数,我想要一个整数。
2 - I wanted a number or a string result to stick into a data structure
2 -我想要一个数字或一个字符串结果,以插入一个数据结构。
so I adapted the original code to produce this derivative:
所以我修改了原始代码来生成这个导数
def string_or_number(s):
try:
z = int(s)
return z
except ValueError:
try:
z = float(s)
return z
except ValueError:
return s
#29
0
use following it handles all cases:-
使用以下方法处理所有情况:-。
import re
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3sd')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3')
#30
-1
To check if the input value is a float
, you can compare the type of the input to a float
要检查输入值是否为浮点数,您可以将输入的类型与浮动进行比较。
def isFloat(s):
realFloat = 0.1
if type(s) == type(realFloat):
return True
else:
return False
Returns:
返回:
False # s = 5
True # s = 1.2345
The original post would actually return True
for s = 5
since it is a number (integer) and you can cast an int
to a float
without a ValueError
. If you are trying to verify that it is an actual float
instead of just a number, you would need to account for that case.
原来的post实际上会返回s = 5,因为它是一个数字(整数),并且您可以在没有ValueError的情况下将int转换为浮点数。如果您试图验证它是一个实际的浮点数,而不是一个数字,那么您需要考虑这个情况。