# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np from numpy import * import math from mpl_toolkits.mplot3d import Axes3D import csv
defReadCsvData(file_name): time = [] loc = [] with open(file_name,'r') as csvfile: reader = csv.reader(csvfile) rows= [row for row in reader] for data in rows: data = np.array(data) time.append(float(data[0])) loc.append([float(data[1]), float(data[2]), float(data[3])]) time = np.array(time) loc = np.array(loc) return time, loc
defsearchInsert(nums, target): start = 0 end = len(nums) - 1 while start <= end: mid = (start + end) // 2 if nums[mid] == target: return mid elif nums[mid] < target: start = mid + 1 else: end = mid - 1 return end + 1 defrIndex(nums, target): n = len(nums) if n == 0: return-1 mid = searchInsert(nums, target) rlist = [] # 保持索引 i, j = -1, n left, rigth = 0, 0# 左右扩展的标志 mxg = float('-inf') if0 < mid < n: # 如果找到了 i, j = mid-1, mid mxg = min(abs(nums[i] - target), abs(nums[j] - target)) left, rigth = 1, 1 elif mid == 0: # 小于最左边的数字 j = mid mxg = abs(nums[j] - target) left, rigth = 0, 1 elif mid == n: # 大于最右边的数字 i = mid-1 mxg = abs(nums[i] - target) left, rigth = 1, 0 while left == 1or rigth == 1: # 两边查找 if i == -1: left = 0 if j == n: rigth = 0 if left == 1and i >= 0: le = abs(nums[i] - target) if le == mxg: rlist = [i] + rlist i -= 1 else: left = 0 if rigth == 1and j < len(nums): ri = abs(nums[j] - target) if mxg == ri: rlist = rlist + [j] j += 1 else: rigth = 0 return rlist