1

I have written a function to perform a calculation based on two values taken from a list and then append the result. I want to know how to adapt this function to perform the calculation on all lists within a list of lists, so far I have met with dismal failure.

This is what I know works so far:

import datetime

Horse = ['Sea Biscuit', '10:57:06', '10:58:42']

#My Function used to get times from the list and calculate speed in MPH

def get_speed():
    time1 = Horse[1]
    time2 = Horse[2]
    d = 1    #Assuming a distance of 1 mile
    FMT = '%H:%M:%S'
    tdelta = datetime.datetime.strptime(time2, FMT) - datetime.datetime.strptime(time1, FMT)
#Convert from timedelta HH:MM:SS format to hours by first converting to a string
    stringtime=str(tdelta)
    parts = stringtime.split(':')
    hours = int(parts[0])*1 + int(parts[1])/60 + int(parts[2])/3600

    speed = round((d/hours),2)

    Horse.append (speed)

get_speed()   

So the list of lists I want to adapt this function to looks like this:

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']]

Many thanks for suggestions and help

robmal
  • 11
  • 1

2 Answers2

1

More elegant class based solutions probably exist, but the quickest way to handle your problem is to just use a for loop:

for horse in Horses:
  def get_speed(horse)

Then you'd jus t want to figure out where you store the outputs.

sahutchi
  • 2,223
  • 2
  • 19
  • 20
  • Indeed... many thanks! It is appending the output into each list which is defeating me right now!! – robmal Feb 02 '15 at 09:58
1

It does depend on how you want to output the data. Although I will answer the question by considering you want the output to also be a list.

Python's Map function might be something you want to look at:

You could do something like:

times = map(get_speed, Horses)

SO, its basically, (syntax)

variable = map(func, list)

Map runs the function on each member of the list.

there's an interesting discussion on Map function in python here: https://stackoverflow.com/a/10973817/1318675 The answer also explains how map actually feels like a nested looped function call.

EDIT: BTW, here's a running code for you. Notice the changes I've made. The sublists need a comma, and the divisions need to be done by decimal numbers, and not integers, otherwise you might get a division by zero when trying to divide for speed in the next step (though, you can also put in a check for zero there!)

LINK: http://ideone.com/0rlCLy

EDIT 2: The OP was using Python 3. In Python 3, maps do not return a list, but return an iterator to the list. Hence, the code need to change to add the following:

return Horse    #Returns the changed horse object back to map

Horses = list(map(get_speed, Horses))    #convert iterator back to list

Running Code here: http://ideone.com/EwcCnQ

Although the map to list conversion can be avoided and directly used as a normal iterator to iterate through the list (Can also be more efficient!)

Community
  • 1
  • 1
ShivamMax
  • 75
  • 9
  • Wow! Thank you for a really speedy and comprehensive response. I will follow the link and do my homework! – robmal Feb 01 '15 at 21:57
  • print (Horses) just showing the original lists without speeds appended - back to the drawing board :-/ – robmal Feb 02 '15 at 07:02
  • The ideone example shows that it does. Did you look at the output there? – ShivamMax Feb 02 '15 at 07:06
  • Hmmm well yes I did... I can see your output print, but mine just prints the original Horses list of lists without appended speeds. It is as though the times = map(get_speed, Horses) is actually not doing anything. *times* is the problem - yes? – robmal Feb 02 '15 at 08:54
  • You actually don't need to put the result into times! (you do recognize that I'm not printing it.) – ShivamMax Feb 02 '15 at 09:25
  • When I run your ideone code I get an error 'Invalid Syntax' and print Horses is shaded red so I put brackets around Horses. This prints the Horses list of lists but doesn't have the calculated speeds appended as in your output ... mightily confused here, which is the problem with being very self-taught! I really appreciate your time and patience ShivamMax – robmal Feb 02 '15 at 10:26
  • Yes hence the brackets for print - but surely there isn't any other version conflicts? – robmal Feb 02 '15 at 12:05
  • Yes! The map function is a bit different in Python 3 than in Python 2.7! Rfer to this link and the link in the accepted answer to know more: http://stackoverflow.com/questions/13638898/how-to-use-filter-map-and-reduce-in-python-3-3-0 – ShivamMax Feb 02 '15 at 21:04
  • Hello @robmal, please have a look at my last edit. Your problem seems to what I had expected about Python 3's different implementation of the map function. – ShivamMax Feb 02 '15 at 21:17
  • WhoHoo! you are a star. My fault for not specifying version 3... lesson learnt – robmal Feb 04 '15 at 08:10