简化代码:def generate_time_series(start,end,t1,t2): current = start end_item = end time_delta = datetime.timedelta(days=t1,hours=t2) time_series = [current] while current < end_item: next1 = current + time_delta time_series.append(next1) current = next1 return time_series time_hour=generate_time_series(data1.loc[0]['FDT_TIME'],data1.loc[len(data1)-1]['FDT_TIME'],0,1) time_day=generate_time_series(data1.loc[0]['FDT_TIME'],data1.loc[len(data1)-1]['FDT_TIME'],1,0) def VALIDITY(timelist,thedata): datalist1=[] datalist2=[] thedata1=thedata.values for i in range(1,len(timelist)): TFlis=[] for j in range(len(thedata1)): if timelist[i-1]<=thedata1[j][2]<timelist[i]: TFlis.append(thedata1[j][6]) datalist1.append(TFlis) for i in datalist1: datalist2.append((i.count("T"))/len(i)) return datalist2 x1=[] for i in range(0,len(VALIDITY(time_hour, data1))): x1.append(i) x2=[] for i in range(0,len(VALIDITY(time_day, data1))): x2.append(i) plt.rcParams['font.sans-serif']=['SimHei'] plt.figure(figsize=(10,5)) plt.title('一周时间内的小时有效性时序图') plt.plot(x1,VALIDITY(time_hour, data1),color='k') plt.xlabel('时间') plt.ylabel('有效性') plt.xticks(np.arange(0,len(x1),24),['2010-04-18','2010-04-19','2010-04-20','2010-04-21','2010-04-22','2010-04-23','2010-04-24']) plt.show() plt.figure(figsize=(10,5)) plt.title('一周时间内的日有效性时序图') plt.plot(x2,VALIDITY(time_day, data1),color='k') plt.xlabel('时间') plt.ylabel('有效性') plt.xticks(np.arange(0,len(x2)),['2010-04-18','2010-04-19','2010-04-20','2010-04-21','2010-04-22','2010-04-23','2010-04-24']) plt.show()
时间: 2023-06-09 10:06:44 浏览: 90
AI: 理解了,你的问题是如何简化以下代码:
def generate_time_series(start,end,t1,t2):
current = start
end_item = end
time_delta = datetime.timedelta(days=t1,hours=t2)
time_series = [current]
while current < end_item:
next1 = current + time_delta
time_series.append(next1)
current = next1
return time_series
time_hour = generate_time_series(data1.loc[0]['FDT_TIME'],
data1.loc[len(data1)-1]['FDT_TIME'],
0, 1)
time_day = generate_time_series(data1.loc[0]['FDT_TIME'],
data1.loc[len(data1)-1]['FDT_TIME'],
1, 0)
def VALIDITY(timelist, thedata):
datalist1 = []
datalist2 = []
thedata1 = thedata.values
for i in range(1, len(timelist)):
TFlis = []
for j in range(len(thedata1)):
if timelist[i-1] <= thedata1[j][2] < timelist[i]:
TFlis.append(thedata1[j][6])
datalist1.append(TFlis)
for i in datalist1:
datalist2.append((i.count("T"))/len(i))
return datalist2
x1 = list(range(len(VALIDITY(time_hour, data1))))
x2 = list(range(len(VALIDITY(time_day, data1))))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(10, 5))
plt.title('一周时间内的小时有效性时序图')
plt.plot(x1, VALIDITY(time_hour, data1), color='k')
plt.xlabel('时间')
plt.ylabel('有效性')
plt.xticks(np.arange(0, len(x1), 24),
['2010-04-18','2010-04-19','2010-04-20','2010-04-21','2010-04-22','2010-04-23','2010-04-24'])
plt.show()
plt.figure(figsize=(10, 5))
plt.title('一周时间内的日有效性时序图')
plt.plot(x2, VALIDITY(time_day, data1), color='k')
plt.xlabel('时间')
plt.ylabel('有效性')
plt.xticks(np.arange(0,len(x2)),
['2010-04-18','2010-04-19','2010-04-20','2010-04-21','2010-04-22','2010-04-23','2010-04-24'])
plt.show()
我的回答是:
def generate_time_series(start,end,days=0,hours=0):
current = start
end_item = end
time_delta = datetime.timedelta(days=days,hours=hours)
time_series = [current]
while current < end_item:
next1 = current + time_delta
time_series.append(next1)
current = next1
return time_series
time_hour = generate_time_series(data1.loc[0]['FDT_TIME'],
data1.loc[len(data1)-1]['FDT_TIME'],
hours=1)
time_day = generate_time_series(data1.loc[0]['FDT_TIME'],
data1.loc[len(data1)-1]['FDT_TIME'],
days=1)
x1 = list(range(len(VALIDITY(time_hour, data1))))
x2 = list(range(len(VALIDITY(time_day, data1))))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(10, 5))
plt.title('一周时间内的小时有效性时序图')
plt.plot(x1, VALIDITY(time_hour, data1), color='k')
plt.xlabel('时间')
plt.ylabel('有效性')
plt.xticks(np.arange(0, len(x1), 24),
['2010-04-18','2010-04-19','2010-04-20','2010-04-21','2010-04-22','2010-04-23','2010-04-24'])
plt.show()
plt.figure(figsize=(10, 5))
plt.title('一周时间内的日有效性时序图')
plt.plot(x2, VALIDITY(time_day, data1), color='k')
plt.xlabel('时间')
plt.ylabel('有效性')
plt.xticks(np.arange(0,len(x2)),
['2010-04-18','2010-04-19','2010-04-20','2010-04-21','2010-04-22','2010-04-23','2010-04-24'])
plt.show()
阅读全文