简体   繁体   English

Python 3-检查数字中的数字是否具有理解力

[英]Python 3 - Check if digit inside a number with if comprehension

This currently works for what I'm trying to do (where 'i' is the number I'm checking to see if it is in the 'digit'): 目前,这适用于我要尝试执行的操作(其中“ i”是我要检查的数字是否在“数字”中):

if str(digit) in str(i):
    count += 1

However, when I try to use an if comprehension I get an invalid syntax: 但是,当我尝试使用if理解时,会得到无效的语法:

count = [count + 1 if str(digit) in str(i)]

I've tried it several different ways using just [count +=1 if...] and count = [count + 1 if...] 我已经尝试过几种不同的方法,仅使用[count + = 1 if ...]和count = [count + 1 if ...]

Can anyone provide some insight into this issue? 谁能提供对此问题的一些见解?

There currently is nothing wrong with the way you are doing it now. 目前,您的操作方式没有任何问题。 Just want to point that out. 只是想指出这一点。 If you are simply trying to do this in one line, maybe this solution is what you are looking for. 如果您只是想在一行中做到这一点,那么也许您正在寻找解决方案。

But to answer your comprehension issue: 但是要回答您的理解问题:

You have two problems going on here that I'd like to point out. 我想指出您在这里遇到的两个问题。

  1. You should not check if you have a digit in your string by trying to cast it to str . 您不应尝试将其strstr来检查字符串中是否包含数字。 Simply use isdigit against the character you are checking. 只需对要检查的字符使用isdigit即可

  2. You cannot use a comprehension the way you are trying to use it. 您不能以试图使用的方式来使用它。 What you have to understand about a comprehension, is that you are creating a new list and taking that new list and assigning it to a variable (in your case count ). 您对理解的理解是,您正在创建一个新列表,并采用该新列表并将其分配给变量(以您的情况count )。 So doing this: 这样做:

    count = [count + 1....] 计数= [计数+ 1 ....]

Really does not make much sense to do. 确实没有多大意义。

What you should do instead if you are looking to do this in a comprehension, 如果您想全面理解该怎么做,应该怎么做,

Iterate over each character in a , for each character, check if it is a digit: 遍历每个字符a字符范围,检查它是否是一个数字:

[c.isdigit() for c in a]

Now, with that above part done. 现在,完成上述部分。 You will have a list of 1s for all digits in your word. 您的单词中所有数字的列表都将为1。 The next step is to sum everything together. 下一步是要sum在一起的一切。 Now, the extra bit of information to pay attention to, is that when calling sum on this, we will lose the square brackets, because we will instead use what is called a generator expression . 现在,需要注意的额外信息是,在对此调用sum时,我们将丢失方括号,因为我们将改用所谓的generator expression

So, with all that said and done. 所以,说完了,做完了。 Your final solution is: 您的最终解决方案是:

a = "fjf7jjjf77773jf3j"


print(sum(c.isdigit() for c in a))

# outputs 7

List comprehension (with square brackets) is used to generate list. 列表理解(带有方括号)用于生成列表。 But, in your case, you are not really generating any list. 但是,就您而言,您实际上并未生成任何列表。 However, if you are trying to write an inline if, try - 但是,如果您要编写内联if,请尝试-

count = count + 1 if str(digit) in str(i) else count

You can just sum the boolean value from character.isdigit() where character is each character in the string. 您可以只对character.isdigit()中的布尔值求和,其中character是字符串中的每个字符。

Consider: 考虑:

>>> s='abc123def456'
>>> [c.isdigit() for c in s]
[False, False, False, True, True, True, False, False, False, True, True, True]
>>> sum(c.isdigit() for c in s)
6

I'm not sure why you would want to get a list in this case because in your first example, you simply incremented an integer variable called count . 我不确定在这种情况下为什么要获取列表,因为在第一个示例中,您只是增加了一个名为count的整数变量。 If you're looking for something more nuanced, you could try using a function like this: 如果您正在寻找更细微的东西,可以尝试使用如下函数:

def count_letter_instance(string="", letter=""):
    return sum([1 for x in string if x == letter])

print(count_letter_instance("hello hello hello", "h"))
print(count_letter_instance("cat cat cat", "c"))

Output: 输出:

3
3

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

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