Python Forum
Find 'greater than' items in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find 'greater than' items in list
#1
rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) with every month separated by a comma. Write code to compute the number of months that have more than 3 inches of rainfall. Store the result in the variable num_rainy_months. In other words, count the number of items with values > 3.0.

#given string
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"

#my attempt
items = rainfall_mi.split(",")
#print (items)
accum = 0
for inches in items:
    if inches > 3.0:
        accum = accum + 1
num_rainy_months = accum
print(num_rainy_months)
Output:
12 (should be 5)
Reply
#2
You need to turn the value inches into a number, at the moment it is comparing a string to a float in the line if inches > 3.0:
convert using float
Reply
#3
In addition to Yoriz suggestion some additional ideas.

You can shorten you code (you don't need accum, you can collect result directly into num_rainy_months; you don't need to assign name to .split(',') result you can iterate over it directly, you can use +=)

rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months = 0
for inches in rainfall_mi.split(','):
    if float(inches) > 3.0:
        num_rainy_months += 1
print(num_rainy_months)
5
As always in programming there are several ways to achieve desired result. Code above is easy to turn into list comprehension or generator.

Generator expression returns 1 for every value in list which is larger than 3.0 and sum() sums them up:

rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months = sum(1 for inches in rainfall_mi.split(',') if float(inches) > 3.0)
List comprehension returns elements which are larger than 3.0 and using len() we get number of months (we can't use len() on generators because they don't have length):

num_rainy_months = len([inches for inches in rainfall_mi.split(',') if float(inches) > 3.0])
There is also built-in function filter(). Introduction of comprehensions made it more or less 'obsolete', but it doesn't mean that one should not know about it. Filter requires functions, below filter object is converted to list just to show the result of filtering:

>>> list(filter(lambda inches: float(inches) > 3.0, rainfall_mi.split(',')))
[' 3.03', ' 3.35', ' 3.46', ' 3.23', ' 3.5']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Collisions for items in a list Idents 3 2,283 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,318 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Removing items in a list cap510 3 2,331 Nov-01-2020, 09:53 PM
Last Post: cap510
  problem with ‘>’ (i.e. greater than) sign AOCL1234 2 1,937 Jun-30-2020, 09:32 PM
Last Post: Yoriz
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,241 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  How do you find the length of an element of a list? pav1983 13 4,847 Jun-13-2020, 12:06 AM
Last Post: pav1983
  Help with Recursive solution,list items gianniskampanakis 8 3,570 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 4,405 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find first letter from a list DariusCG 3 2,503 Nov-27-2019, 11:08 PM
Last Post: ichabod801
  How to add items within a list Mrocks22 2 2,663 Nov-01-2018, 08:46 PM
Last Post: Mrocks22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020