提问人:M.J. 提问时间:11/14/2023 更新时间:11/15/2023 访问量:58
Python 中的 For 循环:在 for 循环之外继续迭代
For loop in Python: continuing iteration outside the for loop
问:
我有一个包含for循环的函数。a_laps是一圈 400 米所需的时间(以秒为单位)。到目前为止,我定义的函数是这样的:
LAP_DISTANCE = 400
def log_track_run(a_laps, a_last_lap_distance):
r_output = ""
total_time = 0
total_distance = 0
n_laps = len(a_laps)
i_lap = None
average_lap_time = None
for i_lap in range(n_laps):
lap_time = a_laps[i_lap]
total_time = total_time + lap_time
total_distance = total_distance + LAP_DISTANCE
average_lap_time = (total_time) / (i_lap + 1)
r_output += f"lap #{i_lap + 1} time = {a_laps[i_lap]}s, average lap time ={round(average_lap_time)}s, {total_distance}m in {total_time}s\n"
如果 a_last_lap_distance = 400 且 a_laps = [68, 70, 74, 68],则得到以下输出,这是正确的输出:
lap #1 time = 68s, average lap time = 68s, 400m in 68s
lap #2 time = 70s, average lap time = 69s, 800m in 138s
lap #3 time = 74sdef log_track_run(a_laps, a_last_lap_distance):
如果a_last_lap_distance小于 400,这意味着最后一轮没有完成,您不能因为圈数未完成而在i_lap上加 1。您必须将 (a_last_lap_distance)/LAP_DISTANCE 相加才能获得与 400 成比例的圈长。我只想更改 a_laps 范围内最后一项的迭代计算 - 在本例中,用于需要 68 秒的最后一圈。
我尝试过以下功能:
def log_track_run(a_laps, a_last_lap_distance):
r_output = ""
total_time = 0
total_distance = 0
n_laps = len(a_laps)
i_lap = None
average_lap_time = None
for i_lap in range(n_laps - 1 ):
lap_time = a_laps[i_lap]
total_time = total_time + lap_time
total_distance = total_distance + LAP_DISTANCE
average_lap_time = (total_time) / (i_lap + 1)
r_output += f"lap #{i_lap + 1} time = {a_laps[i_lap]}s, average lap time = {round(average_lap_time)}s, {total_distance}m in {total_time}s\n"
for i_lap in range(n_laps[-1]):
lap_time = a_laps[i_lap]
total_time = total_time + lap_time
total_distance = total_distance + a_last_lap_distance
average_lap_time = (total_time) / (i_lap + (a_last_lap_distance)/LAP_DISTANCE)
r_output += f"lap #{i_lap + 1} time = {a_laps[i_lap]}s, average lap time {round(average_lap_time)}s, {total_distance}m in {total_time}s\n"
这会产生语法错误。如何修改函数,使第二个循环仅针对范围中的最后一项,同时基于上一个for循环中的上一次迭代?
我试图编写第二个 for 循环,它基于上一个 for 循环,但仅适用于圈中的最后一个项目。
答:
1赞
gog
11/14/2023
#1
一种选择是填充一个列表,其中包含 1 和最后一个元素。然后,通过距离和时间,计算运行平均值:distances
len(a_laps)-1
last_distance/LAP_DIST
zip
def log_track_run(a_laps, a_last_lap_distance):
distances = [1] * (len(a_laps) - 1) + [a_last_lap_distance / LAP_DISTANCE]
total_time = 0
total_dist = 0
for n, (dist, time) in enumerate(zip(distances, a_laps), 1):
total_dist += dist
total_time += time
print(f'lap {n}: avg={total_time / total_dist}')
评论
0赞
M.J.
11/14/2023
感谢您的输入。如何将lap_time合并到语句中,以便输出命令是这样的: r_output += f“lap #{i_lap + 1} time = {a_laps[i_lap]}s, average lap time = {round(average_lap_time)}s, {total_distance}m in {total_time}s\n”
0赞
M.J.
11/14/2023
例如,我必须在输出中打印lap_time,这在我以前的函数中由 a_laps[i_lap] 表示
1赞
JonSG
11/15/2023
#2
我将首先建立一个时间和距离列表,在这些时间和距离中,我将使用这些参数,并弄清楚如何将最后一圈“归一化”为预期距离。last_lap_distance_actual
last_lap_distance_expected
为了完整起见,我将在这里设置一堆变量,以说明我们如何完成每一圈。可能有一些你不需要的。
def log_track_run(laps_times, last_lap_distance_actual=400, last_lap_distance_expected=400):
total_time_actual = 0.0
total_time_normalized = 0.0
total_distance_actual = 0.0
total_distance_normalized = 0.0
total_laps_actual = 0.0
total_laps_normalized = 0.0
## ---------------
## pre generate a list of times and distances
## ---------------
lap_times_and_distances = [[lap_time, last_lap_distance_expected] for lap_time in laps_times]
lap_times_and_distances[-1][1] = last_lap_distance_actual
## ---------------
print("--------------------------")
for lap_time_actual, lap_distance_actual in lap_times_and_distances:
lap_count_actual = lap_distance_actual / last_lap_distance_expected
lap_count_normalized = 1
lap_time_normalized = lap_time_actual * last_lap_distance_expected / lap_distance_actual
lap_distance_normalized = last_lap_distance_expected
total_time_actual += lap_time_actual
total_time_normalized += lap_time_normalized
total_distance_actual += lap_distance_actual
total_distance_normalized += lap_distance_normalized
total_laps_actual += lap_count_actual
total_laps_normalized += lap_count_normalized
avg_lap_time_actual = total_time_actual / total_laps_actual
avg_lap_time_normalized = total_time_normalized / total_laps_normalized
print(f"Lap: {total_laps_actual:.2f} average lap time = {avg_lap_time_actual:.4f}s {int(total_distance_actual)}m in {total_time_actual:.4f}s")
if last_lap_distance_expected != lap_distance_actual:
print(f"\tThis lap was less than {last_lap_distance_expected}m. Projecting results.")
print(f"\tLap: {total_laps_normalized:.2f} average lap time = {avg_lap_time_normalized:.4f}s {int(total_distance_normalized)}m in {total_time_normalized:.4f}s")
print("--------------------------")
## a 1500m race outdoors
log_track_run([68, 70, 74, 68], 300)
## a 200m race indoors
log_track_run([26], 200, 200)
这应该给你:
--------------------------
Lap: 1.00 average lap time = 68.0000s 400m in 68.0000s
Lap: 2.00 average lap time = 69.0000s 800m in 138.0000s
Lap: 3.00 average lap time = 70.6667s 1200m in 212.0000s
Lap: 3.75 average lap time = 74.6667s 1500m in 280.0000s
This lap was less than 400m. Projecting results.
Lap: 4.00 average lap time = 75.6667s 1600m in 302.6667s
--------------------------
--------------------------
Lap: 1.00 average lap time = 26.0000s 200m in 26.0000s
--------------------------
如果需要,您可以了解如何调整打印报表。
评论
1赞
M.J.
11/15/2023
它现在可以工作了,谢谢!
评论