목록에서 가져온 두 값을 기반으로 계산을 수행 한 다음 결과를 추가하는 함수를 작성했습니다. 목록의 목록에있는 모든 목록에 대해이 함수를 적용하여 계산을 수행하는 방법을 알고 싶습니다. 지금까지 나는 음침한 실패를 만났습니다. 제안Python 목록 목록에 MyFunction 적용
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']]
많은 감사를하고
도움 :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()
그래서 목록의 목록이 같은 모습에이 기능을 적용 할 :
이
내가 지금까지 일을 알고있다
참으로 ... 많은 감사합니다! 그것은 지금 나를 격파하고있는 각리스트에 출력을 추가하고있다 !! – robmal