在python中计算mph的最佳方法

时间:2019-01-04 13:47:58

标签: python

经过一段长时间的休息之后,我回到了用python编写程序的过程中,我目前正在编写一个程序,该程序采用纬度和经度值,计算它们之间的距离(以公里和英里/小时为单位),并计算出从一个坐标到另一个坐标,现在我已经有了程序来计算坐标和持续时间之间的距离,结果,我有了两个可以使用的列表:

第一个是当前以HH:MM:SS格式从一个位置到达另一个位置的持续时间列表。如下

timeduration = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']

,还有我根据纬度/经度坐标计算出的距离(以公里为单位)

distances = ['0.6', '0.4', '1.3', '1.7']

我现在想做的是现在在python中计算这些,但是对于最适合此的公式或方法,我有些茫然。

我知道我们是否从两个列表中同时获取了前两个值
例如timeduration[0]distances[0],这应该使我们达到每小时英里数 时速为3.195623 mph或5.14286 km / h。但是我不知道如何将其转换为python。

7 个答案:

答案 0 :(得分:1)

给出一个时间字符串,例如cvlc destination_file.mp4 ,您可以将其转换为小时,如下所示:

timeduration[0]

例如,它将得出0.1197小时的值。您可以对列表中的每个值执行此操作,然后使用这些值创建一个新列表。

然后只需将您的距离值除以小时值(例如h, m, s = timeduration[0].split(":") hours = int(h) + int(m)/60 + int(s)/3600 ),就可以得到以km / hr为单位的速度。

答案 1 :(得分:1)

import datetime
import time

timedurations = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']

distances = ['0.6', '0.4', '1.3', '1.7']

# turn times into hours
hours = [float(timeduration.split(":")[0]) + float(timeduration.split(":")[1]) / 60 + float(timeduration.split(":")[2]) / 3600 for timeduration in timedurations]

# divide each distance by its respective time
speeds = [float(distance) / hour for distance, hour in zip(distances, hours)]


for speed in speeds:
    print(str(speed) + " mph")

答案 2 :(得分:1)

import datetime,time 
timeduration = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']
distances = ['0.6', '0.4', '1.3', '1.7']
speeds=[];y=0
for i in timeduration:
    x = time.strptime(i.split(',')[0],'%H:%M:%S')
    seconds=datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
    hours=float(seconds)/3600
    speeds.append(float(distances[y])/hours)
    y+=1
print speeds

答案 3 :(得分:1)

您可以使用辅助方法将时间转换为小时,然后为转换中的每个子步骤创建一个命名良好的列表:

def get_hours(time_duration):
  h, m, s = time_duration.split(':')
  return float(h) + float(m) / 60 + float(s) / 3600

KM_TO_MI_CONVERSION_FACTOR = 1.609344

time_duration = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']
km_distances = ['0.6', '0.4', '1.3', '1.7']
mi_distances = [float(x)/KM_TO_MI_CONVERSION_FACTOR for x in km_distances]
time_in_hours = [get_hours(x) for x in time_duration]
mi_per_hour = [round(x/y, 7) for x, y in zip(mi_distances, time_in_hours)]
km_per_hour = [round(float(x)/y, 7) for x, y in zip(km_distances, time_in_hours)]
print(mi_per_hour) #[3.1140644, 0.9768281, 2.6508817, 2.7260156]
print(km_per_hour) #[5.0116009, 1.5720524, 4.2661805, 4.3870968]

答案 4 :(得分:1)

您可以为此任务使用enumerate关键字来存储当前的“索引”以进行时间计算或在列表中的位置,例如:

timeduration = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']
distances = ['0.6', '0.4', '1.3', '1.7']
speed = []

for i, t in enumerate(timeduration):
    t = [float(hms)/60**power for power, hms in enumerate(t.split(':'))]
    d = float(distances[i])
    v = d / sum(t)
    speed.append(v)

print(speed)

答案 5 :(得分:1)

如果您愿意使用第三方库,则可以通过Pandas使用矢量化操作。对于较大的输入量,此解决方案应该是有效的。

import pandas as pd

timeduration = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']
distances = ['0.6', '0.4', '1.3', '1.7']

times = pd.to_timedelta(timeduration)  # convert strings to timedeltas
dists = pd.to_numeric(distances)       # convert strings to floats

miles_per_km = 0.621371

res = dists / (times / pd.Timedelta('1 hour')) * miles_per_km

# Float64Index([3.11406348028, 0.976827772926, 2.65088083865, 2.72601470968],
#              dtype='float64')

答案 6 :(得分:0)

使用python括号的解决方案:

timeduration = ['0:07:11', '0:15:16', '0:18:17', '0:23:15']
distances = ['0.6', '0.4', '1.3', '1.7']

temp = [ii.split(':') for ii in timeduration]
timeduration = [(int(ii[0])*3600+int(ii[1])*60+int(ii[2]))/3600 for ii in temp]
distances = [float(ii) for ii in distances]

kmtomile = 0.621371

speed = [kmtomile*distances[ii]/timeduration[ii] for ii in range(len(distances))]

speed的值:

[3.1140634802784226, 0.9768277729257643, 2.650880838650866, 2.726014709677419]