def three_timelong_check(data, plate_number, time, longitude, latitude, status, speed, direction): record = array.array('f', []) k = 0 start = "" end = "" head_time = '2011/04/19 00:00:00' end_time = '2011/04/26 00:00:00' #head_time = datetime.datetime.strptime('2011/04/19 00:00:00', "%Y/%m/%d %H:%M:%S") #end_time = datetime.datetime.strptime('2011/04/26 00:00:00', "%Y/%m/%d %H:%M:%S") for i in range(len(status)): #if datetime.datetime.strptime(time[i], "%Y/%m/%d %H:%M:%S") < end_time and datetime.datetime.strptime(time[i], "%Y/%m/%d %H:%M:%S") > head_time: if time[i] > head_time and time[i] < end_time: if i < len(status) - 1 and status[i] == 0 and status[i+1] == 1: start = time[i+1] elif i < len(status) - 1 and status[i] == 1 and status[i+1] == 0: end = time[i] record[k] = time_cal(start, end) print("{:.3f}分钟".format(time_cal(start, end))) k = k+1 start = "" end = "" print(k)
时间: 2024-04-14 11:28:10 浏览: 79
get-addr-longitude-latitude.py
根据你提供的代码,错误出现在 `record[k] = time_cal(start, end)` 这一行。问题很明显,你在尝试给空的 `record` 数组赋值时,使用了索引 `k`,但是在你的代码中并没有给 `record` 数组分配任何长度。
为了解决这个问题,你需要在循环之前明确指定 `record` 数组的长度。你可以使用以下代码来初始化一个长度为 `len(status)` 的空数组:
```python
record = array.array('f', [0.0] * len(status))
```
然后,你可以将 `record[k] = time_cal(start, end)` 更改为 `record[k-1] = time_cal(start, end)`,因为数组的索引是从 0 开始的,而你的 `k` 是从 1 开始递增的。
修复后的代码应该是这样的:
```python
def three_timelong_check(data, plate_number, time, longitude, latitude, status, speed, direction):
record = array.array('f', [0.0] * len(status))
k = 0
start = ""
end = ""
head_time = '2011/04/19 00:00:00'
end_time = '2011/04/26 00:00:00'
for i in range(len(status)):
if time[i] > head_time and time[i] < end_time:
if i < len(status) - 1 and status[i] == 0 and status[i+1] == 1:
start = time[i+1]
elif i < len(status) - 1 and status[i] == 1 and status[i+1] == 0:
end = time[i]
record[k-1] = time_cal(start, end)
print("{:.3f}分钟".format(time_cal(start, end)))
k = k+1
start = ""
end = ""
print(k)
```
现在,`record[k-1]` 的赋值操作应该不会再引发 "IndexError: array assignment index out of range" 错误了。请注意,这只是修复了索引错误问题,并假定 `time_cal(start, end)` 的结果是浮点数。如果有其他问题,请提供更多相关代码或错误信息以供我进一步帮助。
阅读全文