def timeLeast(start, mid, end,typeFirstSql,typeSecondSql): flag = 1 resultList = [] list = Interface.sort.timeCost(SqlDir.train_table.selectByCity(start, mid,typeFirstSql)) listNext = Interface.sort.timeCost(SqlDir.train_table.selectByCity(mid, end,typeSecondSql)) if len(list)==0 or len(listNext)==0: return elif len(list)<=3 or len(listNext)<=3: flag = 0 for i in list: midTimeStart = tool.timeToMin(i['to_time']) for j in listNext: midTimeEnd = tool.timeToMin(j['start_time']) if (midTimeEnd > midTimeStart + 60): collection = [] stayTime = tool.timeToHoure(midTimeEnd - midTimeStart) wholeCost = tool.timeToHoure(tool.timeToMin(i['cost'])+tool.timeToMin(j['cost'])+midTimeEnd - midTimeStart) wholePrice = '¥'+str(tool.priceToFloat(i['price']) + tool.priceToFloat(j['price'])) list3 = {'stay_time':stayTime,'whole_cost':wholeCost,'whole_price':wholePrice} collection.append(i) collection.append(j) collection.append(list3) resultList.append(collection) if flag == 1: break else: if listNext.index(j) == len(listNext) - 1: break # 处理结果 因为前半程最少加符合最少合起来不一定最少 listPrint = computeResult(resultList) return listPrint
def timeCost(list): for i in range(0, len(list) - 1): for j in range(0, len(list) - i - 1): time = tool.timeToMin(list[j]['cost']) time1 = tool.timeToMin(list[j + 1]['cost']) if time > time1: list[j], list[j + 1] = list[j + 1], list[j] return list
def toTime(list): newList = tool.getTopTen(timeCost(list), 10) for i in range(0, len(newList) - 1): for j in range(0, len(newList) - i - 1): toTime = tool.timeToMin(newList[j]['to_time']) toTime1 = tool.timeToMin(newList[j + 1]['to_time']) if toTime > toTime1: newList[j], newList[j + 1] = newList[j + 1], newList[j] return newList
def multJu(list): for i in range(0, len(list) - 1): for j in range(0, len(list) - i - 1): costTime = tool.timeToMin(list[j]['cost']) costTime1 = tool.timeToMin(list[j + 1]['cost']) price = int(tool.priceToFloat(list[j]['price'])) price1 = int(tool.priceToFloat(list[j + 1]['price'])) multIn = costTime * 0.4 + price multIn1 = costTime1 * 0.4 + price1 if multIn > multIn1: list[j], list[j + 1] = list[j + 1], list[j] return list
def computeResult(list): for i in range(0, len(list) - 1): for j in range(0, len(list) - i - 1): time = tool.timeToMin(list[j][0]['cost']) + tool.timeToMin(list[j][1]['cost']) + ( tool.timeToMin(list[j][1]['start_time']) - tool.timeToMin(list[j][0]['to_time'])) time1 = tool.timeToMin(list[j + 1][0]['cost']) + tool.timeToMin(list[j + 1][1]['cost']) + ( tool.timeToMin(list[j + 1][1]['start_time']) - tool.timeToMin(list[j + 1][0]['to_time'])) if time > time1: list[j], list[j + 1] = list[j + 1], list[j] return tool.getTopTen(list,10)