ONLY DO QUESTIONS 6-8 PLEASE Question 4 (COMPLETE: ''' 4. Let's find the "fireball" sighting(s) that lasted more than ten seconds in US. Print the the datetime and state of each. Put the data in "fball" and print the result. Note: Consider only the US sightings stored in "sightings_us". - Cast the duration in seconds to a float (decimal). - Check if duration is greater than 10. - Check if the shape is "fireball". ''' def is_valid_duration(duration_as_string): #eliminate duration values that don't have a valid duration # your code here try: duration = float(duration_as_string) except ValueError: return False else: return duration fball = [] # iterate through the sightings_us fball = [row for row in sightings_us if is_valid_duration(row["duration (seconds)"]) > 10 and row["shape"] == "fireball"] # output printing for row in fball: print(row['datetime'], row['state']) Question 5 (COMPLETE) ''' 5. Sort the above list by duration. What was the datetime and duration of the longest sighting? Put the sorted list in "fballsorted" and print the result. - Cast the duration in seconds to a float (decimal). - Sort in reverse order. ''' # your code here #cast duration in seconds to a float #sort the list by duration in reverse fballsorted = sorted(fball, key = lambda x: float(x['duration (seconds)']), reverse=True) for row in fballsorted: print((row['datetime'], row['duration (seconds)'])) Question 6 (STUCK) ''' 6. What state had the longest lasting "fireball"? Put the state in "state" and print the result. - Check if the shape is "fireball". - Cast the duration in seconds to a float (decimal). - Get the record with the largest (max) duration in seconds. - Get the state for the record. ''' # your code here #confirmfireball = [row for row in sightings_us if is_valid_duration(row["duration (seconds)"]) and row["shape"] == "fireball"] for row in sightings_us: # get the data for shape='fireball' having float of 'duration (seconds)' > 10 if row["shape"] == "fireball": #confirmfireball = [row for row in sightings_us if row["shape"] == "fireball"] valid_duration = [row for row in sightings_us if is_valid_duration(row["duration (seconds)"])] state = max(valid_duration, key = lambda x: float(x["duration (seconds)"])) print(state['duration (seconds)'], state['state']) Question 7 (STUCK) ''' 7. Let's assume that any sighting (of any shape) of 0 seconds is insignificant. Write code to filter out these extraneous records and get the shortest sighting overall now. Put the minimum duration in "min_duration" and print the result. Use ufosightings Note: Consider all sightings stored in "ufosightings". ''' # your code here Question 8 (STUCK) ''' 8. What are the top 3 shapes sighted, and how many sightings were there for each? Note: Consider all sightings stored in "ufosightings". - Create a new list "sightings_shapes" containing values from the "shape" column in ufosightings. - Create a new dictionary "count" with values of that column as keys and the counts as values. - Get a list of the dictionary keys and values using the items() method. This will return a list of key:value pairs. Sort the list of key:value pairs in reverse order, from greatest (most sightings) to least. Get the top 3 and store in "top3shapes". Print the result. ''' #Create a new list containing values from the "shape" column in ufosightings. # your code here #Create a new dictionary with values of that column as keys and the counts as values. # your code here #Get a list of the dictionary keys and values (use the items() method) and sort them in reverse order, from greatest (most sightings) to least. #Get and print the top 3. # your code here
ONLY DO QUESTIONS 6-8 PLEASE
Question 4 (COMPLETE:
'''
4. Let's find the "fireball" sighting(s) that lasted more than ten seconds in US.
Print the the datetime and state of each. Put the data in "fball" and print the result.
Note: Consider only the US sightings stored in "sightings_us".
- Cast the duration in seconds to a float (decimal).
- Check if duration is greater than 10.
- Check if the shape is "fireball".
'''
def is_valid_duration(duration_as_string): #eliminate duration values that don't have a valid duration
# your code here
try:
duration = float(duration_as_string)
except ValueError:
return False
else:
return duration
fball = []
# iterate through the sightings_us
fball = [row for row in sightings_us if is_valid_duration(row["duration (seconds)"]) > 10 and row["shape"] == "fireball"]
# output printing
for row in fball:
print(row['datetime'], row['state'])
Question 5 (COMPLETE)
'''
5. Sort the above list by duration. What was the datetime and duration of the longest sighting?
Put the sorted list in "fballsorted" and print the result.
- Cast the duration in seconds to a float (decimal).
- Sort in reverse order.
'''
# your code here
#cast duration in seconds to a float
#sort the list by duration in reverse
fballsorted = sorted(fball, key = lambda x: float(x['duration (seconds)']), reverse=True)
for row in fballsorted:
print((row['datetime'], row['duration (seconds)']))
Question 6 (STUCK)
'''
6. What state had the longest lasting "fireball"? Put the state in "state" and print the result.
- Check if the shape is "fireball".
- Cast the duration in seconds to a float (decimal).
- Get the record with the largest (max) duration in seconds.
- Get the state for the record.
'''
# your code here
#confirmfireball = [row for row in sightings_us if is_valid_duration(row["duration (seconds)"]) and row["shape"] == "fireball"]
for row in sightings_us:
# get the data for shape='fireball' having float of 'duration (seconds)' > 10
if row["shape"] == "fireball":
#confirmfireball = [row for row in sightings_us if row["shape"] == "fireball"]
valid_duration = [row for row in sightings_us if is_valid_duration(row["duration (seconds)"])]
state = max(valid_duration, key = lambda x: float(x["duration (seconds)"]))
print(state['duration (seconds)'], state['state'])
Question 7 (STUCK)
'''
7. Let's assume that any sighting (of any shape) of 0 seconds is insignificant.
Write code to filter out these extraneous records and get the shortest sighting overall now.
Put the minimum duration in "min_duration" and print the result.
Use ufosightings
Note: Consider all sightings stored in "ufosightings".
'''
# your code here
Question 8 (STUCK)
'''
8. What are the top 3 shapes sighted, and how many sightings were there for each?
Note: Consider all sightings stored in "ufosightings".
- Create a new list "sightings_shapes" containing values from the "shape" column in ufosightings.
- Create a new dictionary "count" with values of that column as keys and the counts as values.
- Get a list of the dictionary keys and values using the items() method. This will return a list of key:value pairs.
Sort the list of key:value pairs in reverse order, from greatest (most sightings) to least.
Get the top 3 and store in "top3shapes". Print the result.
'''
#Create a new list containing values from the "shape" column in ufosightings.
# your code here
#Create a new dictionary with values of that column as keys and the counts as values.
# your code here
#Get a list of the dictionary keys and values (use the items() method) and sort them in reverse order, from greatest (most sightings) to least.
#Get and print the top 3.
# your code here
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images