Reputation: 87
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:
NameError: name 'j' is not defined
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
else:
count_list += 0
return count_list
count_greater([12,0,20,34,0,20],3)
How should I approach this? I am unsure on how to complete the rest of the logic.
Upvotes: 0
Views: 66
Reputation: 48077
Your for
loop is syntactically incorrect. You need to iterate over the just xs
list and check with the value of v
. For example to get number greater than and equal to v
in your xs
:
def count_get_greater_item(xs,v):
greater_than_v = []
for item in xs:
if item >= v:
greater_than_v.append(item)
return greater_than_v
count_above([22,0,30,44,0,30],5)
However, for finding the duplicates (my understanding is the number occurring in the list more than once), you may use collections.Counter()
as:
import collections
duplicate_list = []
for item, count in collections.Counter(x).items():
if count > v:
duplicate_list.append(item)
OR, in one line via list comprehension as:
duplicate_list = [item for item, count in collections.Counter(xs).items() if count > v]
where xs
and v
are holding the same value as is mentioned in question
Upvotes: 0
Reputation: 1691
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
count_greater([12,0,20,34,0,20],3)
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.
Upvotes: 0
Reputation: 519
You can use a list comprehension to get conditional occurrence counts and sets to identify the (total) number of duplicates:
vals = [22,0,30,44,0,30]
ceil = 5
over_ceil_count = len([v for v in vals if v > ceil])
dup_count = len(vals) - len(set(vals))
The set automatically removes duplicates, so the difference between the length of the set and the length of the original list tells you how many duplicate values you started with.
Upvotes: 0
Reputation: 77847
Use the built-in capabilities of the language:
def count_greater(xs,v):
return sum([item >= v for item in xs])
print (count_greater([12,0,20,34,0,20],3))
Output is 4, as expected.
In your code, note that count += 0
does absolutely nothing; remove that and the else. As you suggested, just use v; there's no need to iterate j over a construct that you know is exactly one item long.
Upvotes: 0
Reputation: 756
v is not a list, but a single number, so you do not need to iterate over it.
So this would be sufficient:
for i in (xs):
if i > v:
count_list += 1
There are alternative approaches you could take. For instance, use filter(function, list), like so:
list_greater = filter(lambda d: d < v, xs)
count_greater = len(list_greater)
This approach has the advantage that you can work with the filtered numbers, too, not just count them.
Upvotes: 1
Reputation: 1123
as I understand the working code is it count the values in the list more than v
def count_above(xs,v):
count_list = 0
for i in xs: # for each i in the list which is xs
if i > v:
count_list += 1
else: # take care of else space should be under if dircitly
count_list += 0
return count_list
count_above([22,0,30,44,0,30],5)
you should take care of your spaces and you loop on a list for each i n the list as i mention in code above
Upvotes: 0