if len(dataList) > 0: oldDId = dataList[-1][0] + '_' + dataList[-1][1] # 获取旧的ID键 oldFTime = dataList[-1][-1] # 获取时间 if oldDId != dId or self.diffSec(fTime, oldFTime) > 0: # 计算整车数据 if len(dataList) > 0: self.doData(dataList) count = 0 dataList = [[fTime]] count = count + 1 if count > 1: dataList.append( [fTime])
时间: 2024-04-18 07:33:37 浏览: 42
这段代码的作用是根据条件计算整车数据。首先,它检查`dataList`列表的长度是否大于0,是,则获取最后一个元素的ID键和时间。接下来,它将检查旧的ID键是否与当前的ID键不同,或者当前时间与旧时间之间的差值是否大于0。如果是这样,它会执行`self.doData(dataList)`来计算整车数据。然后,它重置计数器`count`为0,并将当前时间添加到`dataList`列表中作为新的数据点。最后,如果计数器`count`大于1,则将当前时间作为新的数据点添加到`dataList`列表中。
相关问题
约瑟夫环改错class Node: def __init__(self,data): self.data=data self.next=Noneclass linklist: def __init__(self): self.head=None self.data=None def isEmpty(self): if self.head: return False else: return True def length(self): if self.isEmpty(): return 0 else: t = self.head n = 1 while t.next: if t.next == self.head: break t = t.next n = n + 1 return n def addhead(self,data): node = Node(data) if self.isEmpty(): self.head = node self.tail = self.head else: node.next = self.head self.head = node self.tail.next = self.head def addtail(self,data): node=Node(data) if self.isEmpty(): self.addhead(data) else: t=self.head n=1 l=self.length() while n<l: n=n+1 t=t.next t.next=node node.next=self.head self.tail=node def delete(self,index): if self.isEmpty(): print("The linked list is empty") else: t = self.head l = self.length() if index == 0: self.head = t.next self.tail.next = self.head elif index == l - 1: n = 1 while n < l - 1: t = t.next n = n + 1 t.next = self.head self.tail = t elif index > l - 1: print("Out of range") elif index < 0: print("Wrong operation") else: n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = a def insert(self,data,index): l = self.length() if index == 0 or self.isEmpty(): self.addhead(data) elif index >= l: self.addtail(data) else: node = Node(data) t = self.head n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next t.next = node node.next = a def search(self,a): t=self.head for i in range(a): t=t.next return t.data def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next != self.head: t = t.nextn,p=map(int,input().split(' '))data=[]p=p-1for i in range(1,n+1): data.append(i)print(data)datalist=[]for i in range(len(data)): datalist.append(int(data[i]))link=linklist()link.form(datalist)a=pb=[]while link.length()>0: b.append(link.search(a)) link.delete(a) a=a+p while a>=link.length(): a=a-link.length()print(b)
改错如下:
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkList:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
if self.head:
return False
else:
return True
def length(self):
if self.isEmpty():
return 0
else:
t = self.head
n = 1
while t.next:
if t.next == self.head:
break
t = t.next
n += 1
return n
def addhead(self, data):
node = Node(data)
if self.isEmpty():
self.head = node
self.tail = self.head
else:
node.next = self.head
self.head = node
self.tail.next = self.head
def addtail(self, data):
node = Node(data)
if self.isEmpty():
self.addhead(data)
else:
t = self.head
n = 1
l = self.length()
while n < l:
n += 1
t = t.next
t.next = node
node.next = self.head
self.tail = node
def delete(self, index):
if self.isEmpty():
print("The linked list is empty")
else:
t = self.head
l = self.length()
if index == 0:
self.head = t.next
self.tail.next = self.head
elif index == l - 1:
n = 1
while n < l - 1:
t = t.next
n += 1
t.next = self.head
self.tail = t
elif index > l - 1:
print("Out of range")
elif index < 0:
print("Wrong operation")
else:
n = 1
while n < index:
t = t.next
n += 1
a = t.next.next
t.next = a
def insert(self, data, index):
l = self.length()
if index == 0 or self.isEmpty():
self.addhead(data)
elif index >= l:
self.addtail(data)
else:
node = Node(data)
t = self.head
n = 1
while n < index:
t = t.next
n += 1
a = t.next
t.next = node
node.next = a
def search(self, a):
t = self.head
for i in range(a):
t = t.next
return t.data
def form(self, datalist):
self.addhead(datalist[0])
for i in range(1, len(datalist)):
self.addtail(datalist[i])
t = self.head
while t.next != self.head:
t = t.next
n, p = map(int, input().split(' '))
data = [i for i in range(1, n+1)]
print(data)
datalist = []
for i in range(len(data)):
datalist.append(data[i])
link = LinkList()
link.form(datalist)
a = p-1
b = []
while link.length() > 0:
b.append(link.search(a))
link.delete(a)
a += p-1
while a >= link.length():
a -= link.length()
print(b)
```
请问如何利用pandas函数代替下列代码: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)
可以使用pandas的date_range函数代替generate_time_series函数:
time_hour = pd.date_range(start=data1.loc[0]['FDT_TIME'], end=data1.loc[len(data1)-1]['FDT_TIME'], freq='H')
time_day = pd.date_range(start=data1.loc[0]['FDT_TIME'], end=data1.loc[len(data1)-1]['FDT_TIME'], freq='D')
然后,使用pandas的groupby和agg函数可以简化VALIDITY函数:
def VALIDITY(timelist, thedata):
thedata1 = thedata.groupby(pd.cut(thedata['FDT_TIME'], timelist)).agg({'TF': lambda x: x.tolist().count("T")/len(x.tolist())}).values.tolist()
datalist2 = [i[0] for i in thedata1]
return datalist2
x1 = list(range(len(VALIDITY(time_hour, data1))))
x2 = list(range(len(VALIDITY(time_day, data1))))
阅读全文