python做两个list的时间最短匹配

python做两个list的时间最短匹配

 

 

 

方案一:

cfl = dirCellInfo['FileNamelist']#cellfilenamelist
ctl = dirCellInfo['FileTime']#celltimelist
mtl = dirMiceInfo['FileTime']#micetimelist
mi = []#mice index,匹配上的Mice index
c = 0
for t in ctl:#t is currnet cell time
    tmp = [abs(i - t) for i in mtl] #对 mtl都减去当前的 cell time, tmp is temp
    mi.append(tmp.index(min(tmp)))#得到了当前和Cell最匹配的index
    self.teLog.append(cfl[c] + "\t-->time:" + str(t) + "\t-->MiceIndex:" + str(mi[-1]) + "\t-->delt:" + str(min(tmp)))
    c = c + 1

 

 

     -->time:53713519     -->index:48     -->delt:12
     -->time:53713612     -->index:50     -->delt:0
     -->time:53713713     -->index:53     -->delt:10

 

方案二,用矩阵操作,效率能提升10左右

time_start=time.time()
ctlRM = np.tile(np.array(ctl),(np.shape(mtl)[0],1))
mtlRM = np.tile(np.array(mtl), (np.shape(ctl)[0], 1))#Mice time list rematrix
mtlRMT = mtlRM.T#Mice time list rematrix transpose
dm = abs(ctlRM - mtlRMT)
mi = np.argmin(dm, axis=0)#mice index
time_end=time.time()
print('111totally cost',time_end-time_start)

posted @ 2020-09-22 10:58  bH1pJ  阅读(37)  评论(0编辑  收藏  举报