简体   繁体   English

返回值大于目标值

[英]returning values greater than the target value

I'm trying to figure out how to change this code. 我试图弄清楚如何更改此代码。 Instead of counting the number of times that the target value appears, I want to be able to return the number of values in the list that are greater than the target value. 我希望不计算目标值出现的次数,而是希望能够返回列表中大于目标值的值的数量。

def countTarget2(myList, target):
    counter = 0
    for element in myList:
        if element == target:
            counter = counter + 1
    return counter

Do I change the 5 line too? 我也要更改5行吗?

counter > counter + 1

The change is fairly trivial: 更改相当简单:

if element > target:  # if the element is greater than the target

Don't forget to properly indent the line that follows this. 不要忘了正确缩进此行。

Note that the most Pythonic way to write this function would be to use sum() : 请注意,编写此函数的最Pythonic方法是使用sum()

def countTarget2(myList, target):
    return sum(1 for element in myList if element > target)

You can use a list comprehension approach: 您可以使用列表理解方法:

def countTarget2(myList, target):
    return len([element for element in myList if element > target])

print countTarget2([1,2,3,4], 2)  # prints 2

UPD: UPD:

This is actually a short form of: 这实际上是以下内容的简称:

def countTarget2(myList, target):
    l = []
    for element in myList:
        if element > target:
             l.append(element)
    return len(l)

So, yes, there is an intermediate list - generator expression is a better choice. 因此,是的,这里有一个中间列表-生成器表达式是一个更好的选择。

No, you would only change line 4 不,您只需要更改line 4

def countTarget2(myList, target):
    counter = 0
    for element in myList:
        if element > target:
            counter = counter + 1
    return counter

Note that the if statement requires indentation on line 5 请注意, if语句在line 5要求缩进

You change line 4 to if element > target: : 您将第4行更改为if element > target: ::

def countTarget2(myList, target):
    counter = 0
    for element in myList:
        if element > target:
            counter += 1
    return counter

or, you use the more functional (but probably hard to understand for a beginner): 或者,您使用功能更强大(但对于初学者来说可能很难理解):

def countTarget2(myList, target):
    return len(x for x in myList if x < target)

PS I've also changed counter = counter + 1 to the nicer looking counter += 1 . PS我也将counter = counter + 1更改为外观更好的counter += 1

No in this case you are changing the equivalence test to a greater than test. 在这种情况下,您不会将等效检验更改为大于检验。

You are currently testing element == target which will execute the (properly indented) counter = counter+1 line on every equivalence. 您当前正在测试element == target ,它将在每个等效项上执行(适当缩进) counter = counter+1行。

To do a greater than test you'd need to change the test to element > target . 要进行比测试更大的测试,您需要将测试更改为element > target

The code would look like: 代码如下所示:

def countTarget2(myList, target):
    counter = 0
    for element in myList:
        if element == target:
            counter = counter + 1 # properly indented, dont forget this!
    return counter

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

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