各位大神们,下面的代码速度好慢,有什么办法解决一下吗?还是我写的有问题,帮忙看一下,谢谢大家。
import pandas as pdimport csv
import numpy as np
import time
start = time.time()
def list_of_groups(init_list, children_list_len):
list_of_groups = zip(*(iter(init_list),) * children_list_len)
end_list = [list(i) for i in list_of_groups]
count = len(init_list) % children_list_len
end_list.append(init_list[-count:]) if count !=0 else end_list
return end_list
# 获取id
with open("./A.csv") as f:
csv_readlines=csv.reader(f,delimiter=',')
taxi_id = []
for row in csv_readlines:
VEHICLEOID = row[1]
if VEHICLEOID in taxi_id:
continue
else:
taxi_id.append(VEHICLEOID)
taxi_id.remove("VEHICLEOID")
print(taxi_id)
print(len(taxi_id))
end = time.time()
print("Elapsed (after compilation) = %s" % (end - start))
all_count = []
for tx_id in range(len(taxi_id)):
LoadStatus = []
with open("./A.csv") as f:
csv_readlines=csv.reader(f,delimiter=',')
for rows in csv_readlines:
if rows[1] == taxi_id[tx_id]:
LoadStatus.append(rows[14])
print(LoadStatus)
i = 0
taxi_count = []
while i <= len(LoadStatus) - 3:
i += 1
if LoadStatus[i] == "0" and LoadStatus[i + 1] == "1":
taxi_count.append(i+1)
#i += 1
if len(taxi_count):
if LoadStatus[i] == "1" and LoadStatus[i + 1] == "0":
taxi_count.append(i)
list_c = [0] * len(LoadStatus)
taxi_num = list_of_groups(taxi_count,2)
taxi_sta = 1
if len(taxi_num) and len(taxi_count)%2 == 0:
for i in taxi_num:
#print(i[1] - i[0] + 1)
if i[1] - i[0] + 1 >= 15:
list_c[i[0]:i[1]+1] = [taxi_sta] * (i[1] - i[0] + 1)
taxi_sta += 1
else:
list_c[i[0]:i[1] + 1] = [0] * (i[1] - i[0] + 1)
if len(taxi_num) and len(taxi_count)%2 == 1:
for i in taxi_num:
if len(i) == 2 and i[1] - i[0] + 1 >= 15:
#print(i[1] - i[0] + 1)
list_c[i[0]:i[1]+1] = [taxi_sta] * (i[1] - i[0] + 1)
taxi_sta += 1
else:
list_c[i[0]:len(LoadStatus)+1] = [0] * (len(LoadStatus) - i[0])
all_count.extend(list_c)
print(all_count)