Check if a string contains a number

后端 未结 16 1512
無奈伤痛
無奈伤痛 2020-11-22 11:14

Most of the questions I\'ve found are biased on the fact they\'re looking for letters in their numbers, whereas I\'m looking for numbers in what I\'d like to be a numberless

16条回答
  •  日久生厌
    2020-11-22 11:43

    I'm surprised that no-one mentionned this combination of any and map:

    def contains_digit(s):
        isdigit = str.isdigit
        return any(map(isdigit,s))
    

    in python 3 it's probably the fastest there (except maybe for regexes) is because it doesn't contain any loop (and aliasing the function avoids looking it up in str).

    Don't use that in python 2 as map returns a list, which breaks any short-circuiting

提交回复
热议问题