fork download
  1. import datetime
  2.  
  3. # Horse = ['Sea Biscuit', '10:57:06', '10:58:42']
  4.  
  5. #My Function used to get times from the list and calculate speed in MPH
  6.  
  7. def get_speed(Horse):
  8. time1 = Horse[1]
  9. time2 = Horse[2]
  10. d = 1 #Assuming a distance of 1 mile
  11. FMT = '%H:%M:%S'
  12. tdelta = datetime.datetime.strptime(time2, FMT) - datetime.datetime.strptime(time1, FMT)
  13. #Convert from timedelta HH:MM:SS format to hours by first converting to a string
  14. stringtime=str(tdelta)
  15. parts = stringtime.split(':')
  16. hours = int(parts[0])*1.0 + int(parts[1])/60.0 + int(parts[2])/3600.0
  17.  
  18. speed = round((d/hours),2)
  19.  
  20. Horse.append (speed)
  21.  
  22. return Horse
  23.  
  24.  
  25. Horses = [['Sea Biscuit', '10:57:06', '10:58:42'],['Red Rum', '10:57:06', '10:59:02'],['Blazing saddles', '10:57:06', '10:59:16']]
  26.  
  27. print(list(map(get_speed, Horses)))
Success #stdin #stdout 0.04s 9416KB
stdin
Standard input is empty
stdout
[['Sea Biscuit', '10:57:06', '10:58:42', 37.5], ['Red Rum', '10:57:06', '10:59:02', 31.03], ['Blazing saddles', '10:57:06', '10:59:16', 27.69]]