简体   繁体   English

Python:如果单词包含数字

[英]Python: if a word contains a digit

I'm writing a function that will take a word as a parameter and will look at each character and if there is a number in the word, it will return the word 我正在编写一个函数,该函数将单词作为参数并查看每个字符,如果单词中有数字,它将返回单词

This is my string that I will iterate through 'Let us look at pg11.' 这是我要迭代的字符串,“让我们看看pg11”。 and I want to look at each character in each word and if there is a digit in the word, I want to return the word just the way it is. 我想查看每个单词中的每个字符,如果单词中有一个数字,我想按原样返回单词。

import string

def containsDigit(word):

    for ch in word:
        if ch == string.digits
        return word
if any(ch.isdigit() for ch in word):
    print word, 'contains a digit'

To make your code work use the in keyword (which will check if an item is in a sequence), add a colon after your if statement, and indent your return statement. 为了使您的代码正常工作,请使用in关键字(它将检查项目是否在序列中),在if语句之后添加一个冒号,然后对return语句进行缩进。

import string

def containsDigit(word):

    for ch in word:
        if ch in string.digits:
            return word

Why not use Regex? 为什么不使用Regex?

>>> import re
>>> word = "super1"
>>> if re.search("\d", word):
...     print("y")
...
y
>>>

So, in your function, just do: 因此,在您的函数中,只需执行以下操作:

import re
def containsDigit(word):
    if re.search("\d", word):
        return word
print(containsDigit("super1"))

output: 输出:

'super1'

You are missing a colon: 您缺少一个冒号:

for ch in word:
    if ch.isdigit(): #<-- you are missing this colon
        print "%s contains a digit" % word
        return word

Often when you want to know if "something" contains "something_else" sets may be usefull. 通常,当您想知道“某物”是否包含“ something_else”集时可能有用。

digits = set('0123456789')

def containsDigit(word):  
    if set(word) & digits:  
        return word

print containsDigit('hello')

If you desperately want to use the string module. 如果您拼命想使用string模块。 Here is the code: 这是代码:

import string
def search(raw_string):
    for raw_array in string.digits:
        for listed_digits in raw_array:
            if listed_digits in raw_string:
                return True
    return False

If I run it in the shell here I get the wanted resuts. 如果我在外壳中运行它,则会得到所需的结果。 (True if contains. False if not) (如果包含,则为True;否则为False)。

>>> search("Give me 2 eggs")
True
>>> search("Sorry, I don't have any eggs.")
False

Code Break Down 代码分解

This is how the code works 代码就是这样工作的

The string.digits is a string. string.digits是一个字符串。 If we loop through that string we get a list of the parent string broke down into pieces. 如果我们遍历该字符串,则会得到父字符串的列表,该列表分为几部分。 Then we get a list containing every character in a string with'n a list. 然后,我们得到一个包含字符串中的每个字符的列表,列表中没有。 So, we have every single characters in the string! 因此,我们在字符串中有每个字符! Now we loop over it again! 现在,我们再次遍历它! Producing strings which we can see if the string given contains a digit because every single line of code inside the loop takes a step, changing the string we looped through. 产生字符串,我们可以看到给定的字符串是否包含数字,因为循环内的每一行代码都需要执行一个步骤,从而更改了我们循环通过的字符串。 So, that means ever single line in the loop gets executed every time the variable changes. 因此,这意味着每次变量更改时都会执行循环中的单行。 So, when we get to; 所以,当我们到达时; for example 5. It agains execute the code but the variable in the loop is now changed to 5. It runs it agin and again and again until it finally got to the end of the string. 例如5。它再次执行代码,但是循环中的变量现在更改为5。它重新开始运行,直到最终到达字符串末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM