Alan Wakke Alan Wakke - 1 year ago 82
Python Question

Finding the min

I'm a beginner in python and having trouble with this function. I'm trying to count the values in list xs that are greater than v as well as the duplicates. I'm getting an error for line 3, should I get rid of j and just use I to represent the values in the list?

Code below that I have:

def count_greater(xs,v):
count_list = 0
for i in (xs) and j in (v):
if i > j:
count_list += 1
count_list += 0
return count_list

Answer Source

I think this is what you are after:

def count_greater(xs,v):
    count = 0
    for i in xs:
        if i >= v:
            count += 1
    return count

It returns the number of times that a value in the list is equal to or greater than the second argument (3 in the example). The value returned in this case is 4.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download