检查字符串是否包含数字

Jam*_*lly 167 python string

我发现的大多数问题都是因为他们正在寻找他们的数字中的字母,而我正在寻找我想成为无数字符串的数字.我需要输入一个字符串并检查它是否包含任何数字,如果它确实拒绝它.

如果所有字符都是数字,则该函数isdigit()仅返回True.我只是想看看用户是否输入了一个数字,如"我拥有1只狗"之类的句子.

有任何想法吗?

the*_*eye 251

您可以使用any函数,具有此str.isdigit功能

>>> def hasNumbers(inputString):
...     return any(char.isdigit() for char in inputString)
... 
>>> hasNumbers("I own 1 dog")
True
>>> hasNumbers("I own no dog")
False
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用正则表达式,如下所示

>>> import re
>>> def hasNumbers(inputString):
...     return bool(re.search(r'\d', inputString))
... 
>>> hasNumbers("I own 1 dog")
True
>>> hasNumbers("I own no dog")
False
Run Code Online (Sandbox Code Playgroud)

  • 最初的正则表达式不会检测负数吗? (12认同)
  • @thefourtheye:-1仍然是一个数字.它是一个短划线,后跟数字'1' (4认同)
  • 负数呢? (2认同)
  • @confused00 不,“\d”只会匹配“0”到“9”范围内的单个数字。 (2认同)

aIK*_*Kid 39

您可以使用组合anystr.isdigit:

def num_there(s):
    return any(i.isdigit() for i in s)
Run Code Online (Sandbox Code Playgroud)

True如果字符串中存在数字,则函数将返回,否则返回False.

演示:

>>> king = 'I shall have 3 cakes'
>>> num_there(king)
True
>>> servant = 'I do not have any cakes'
>>> num_there(servant)
False
Run Code Online (Sandbox Code Playgroud)


zyx*_*xue 26

https://docs.python.org/2/library/re.html

你应该更好地使用正则表达式.它快得多.

import re

def f1(string):
    return any(i.isdigit() for i in string)


def f2(string):
    return re.search('\d', string)


# if you compile the regex string first, it's even faster
RE_D = re.compile('\d')
def f3(string):
    return RE_D.search(string)

# Output from iPython
# In [18]: %timeit  f1('assdfgag123')
# 1000000 loops, best of 3: 1.18 µs per loop

# In [19]: %timeit  f2('assdfgag123')
# 1000000 loops, best of 3: 923 ns per loop

# In [20]: %timeit  f3('assdfgag123')
# 1000000 loops, best of 3: 384 ns per loop
Run Code Online (Sandbox Code Playgroud)


K24*_*246 24

使用

str.isalpha() 

参考:https://docs.python.org/2/library/stdtypes.html#str.isalpha

如果字符串中的所有字符都是字母并且至少有一个字符,则返回true,否则返回false.

  • 还有其他类型的字符,而不是字母和数字 - 例如,`'_'.isalpha()`是False. (6认同)

Hai*_*ini 7

您可以对String中的每个字符应用函数isdigit().或者你可以使用正则表达式.

另外我发现如何在Python中的字符串中找到一个数字?以非常合适的方式返回数字.以下解决方案来自该问题的答案.

number = re.search(r'\d+', yourString).group()
Run Code Online (Sandbox Code Playgroud)

或者:

number = filter(str.isdigit, yourString)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看正则表达式文档:http://docs.python.org/2/library/re.html

编辑:返回实际数字,而不是布尔值,因此上面的答案对于您的情况更正确

第一种方法将返回第一个数字和后续连续数字.因此,1.56将返回1. 10,000将返回10. 0207-100-1000将返回0207.

第二种方法不起作用.

要提取所有数字,点和逗号,而不是丢失非连续数字,请使用:

re.sub('[^\d.,]' , '', yourString)
Run Code Online (Sandbox Code Playgroud)


Rau*_*aul 7

我会让@zyxue 的答案更明确一点:

import re    

RE_D = re.compile('\d')

def has_digits(string):
    res = RE_D.search(string)
    return res is not None

has_digits('asdf1')
Out: True

has_digits('asdf')
Out: False
Run Code Online (Sandbox Code Playgroud)

这是 @zyxue 在答案中提出的解决方案中具有最快基准的解决方案。


Jea*_*bre 6

我很惊讶没有人提到any和的这种组合map

def contains_digit(s):
    isdigit = str.isdigit
    return any(map(isdigit,s))
Run Code Online (Sandbox Code Playgroud)

在 python 3 中,它可能是最快的(可能除了正则表达式)是因为它不包含任何循环(并且对函数使用别名可以避免在 中查找它str)。

不要在 python 2 中使用它作为map返回 a list,这会破坏any短路