有没有更好的方法来查找字符串是否包含数字? [英] Is there a better way to find if string contains digits?

查看:44
本文介绍了有没有更好的方法来查找字符串是否包含数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理同时包含数字和字母数字的字符串,或者只包含数字但不仅仅是字母的字符串.为了测试错误匹配,我需要检查字符串是否至少包含一位数字,如果不包含则打印一条错误消息.我一直在使用以下代码:

s = '0798237 sh 523-123-asdjlh'def contains_digits(s):对于列表中的字符:如果 char.isdigit():返回真休息返回错误如果 contains_digits(s) == True:印刷别的:打印错误"

是否有更pythonic或更简单的方法来做到这一点,或者这是否足够?另外,我不能只检查字符串是否为字母数字,因为字符串可能包含各种符号('-'、空格等)

解决方案

这是正则表达式只是其中之一:

_digits = re.compile('\d')def contains_digits(d):return bool(_digits.search(d))

小演示:

<预><代码>>>>_digits = re.compile('\d')>>>def contains_digits(d):... return bool(_digits.search(d))...>>>contains_digits('0798237 sh 523-123-asdjlh')真的>>>contains_digits('sh asdjlh')错误的

您可以将 any 方法与 .isdigit() 一起使用,如@Wallacolloo 的回答,但这比简单的正则表达式要慢:

<预><代码>>>>导入时间>>>timeit.timeit("contains_digits('0798237 sh 523-123-asdjlh')", 'from __main__ import contains_digits')0.77181887626647949>>>timeit.timeit("contains_digits_any('0798237 sh 523-123-asdjlh')", '从 __main__ 导入 contains_digits_any')1.7796030044555664

if 方法与正则表达式相同:

<预><代码>>>>timeit.timeit("contains_digits_if('0798237 sh 523-123-asdjlh')", 'from __main__ import contains_digits_if')0.87261390686035156

但如果数字出现在文本中,事情会变得更糟:

<预><代码>>>>timeit.timeit("contains_digits('asdjlhtaheoahueoaea 11 thou')", '从 __main__ 导入 contains_digits')1.202538013458252>>>timeit.timeit("contains_digits_any('asdjlhtaheoahueoaea 11 thou')", '从 __main__ 导入 contains_digits_any')5.0348429679870605>>>timeit.timeit("contains_digits_if('asdjlhtaheoahueoaea 11 thou')", '从__main__ 导入 contains_digits_if')3.707183837890625

在 Mac OS X 10.7 上的 python 2.6 上测试的时间.

I'm working with strings that contain both digits and alphanumerics, or just digits, but not just alphas. In order to test for false matches, I need to check if the strings contain at least one digit, printing an error message if it doesn't. I have been using the following code:

s = '0798237 sh 523-123-asdjlh'

def contains_digits(s):
    for char in list(s):
        if char.isdigit():
            return True
            break
    return False

if contains_digits(s) == True:
    print s
else:
    print 'Error'

Is there a more pythonic or simpler way to do so, or does this suffice? Also, I can't just check to see if the string is alphanumeric, because the string may contain various symbols ('-', spaces, etc.)

解决方案

This is one of those places where a regular expression is just the thing:

_digits = re.compile('\d')
def contains_digits(d):
    return bool(_digits.search(d))

Little demo:

>>> _digits = re.compile('\d')
>>> def contains_digits(d):
...     return bool(_digits.search(d))
... 
>>> contains_digits('0798237 sh 523-123-asdjlh')
True
>>> contains_digits('sh asdjlh')
False

You could use the any method with .isdigit() as described in @Wallacolloo's answer, but that's slower than the simple regular expression:

>>> import timeit
>>> timeit.timeit("contains_digits('0798237 sh 523-123-asdjlh')", 'from __main__ import contains_digits')
0.77181887626647949
>>> timeit.timeit("contains_digits_any('0798237 sh 523-123-asdjlh')", 'from __main__ import contains_digits_any')
1.7796030044555664

The if method is on par with the regular expression:

>>> timeit.timeit("contains_digits_if('0798237 sh 523-123-asdjlh')", 'from __main__ import contains_digits_if')
0.87261390686035156

But things get worse if the digits appear late in the text:

>>> timeit.timeit("contains_digits('asdjlhtaheoahueoaea 11 thou')", 'from __main__ import contains_digits')
1.202538013458252
>>> timeit.timeit("contains_digits_any('asdjlhtaheoahueoaea 11 thou')", 'from __main__ import contains_digits_any')
5.0348429679870605
>>> timeit.timeit("contains_digits_if('asdjlhtaheoahueoaea 11 thou')", 'from __main__ import contains_digits_if')
3.707183837890625

Timings tested on python 2.6 on Mac OS X 10.7.

这篇关于有没有更好的方法来查找字符串是否包含数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆