2019年1月24日 星期四

[python] 精簡美麗的 Python

def get_nearest(list_A, list_B):
    list_loc_nearest = []
    for loc in list_A:
        list_loc_nearest.append(min(list_B, key=lambda x:abs(x-loc)))
        '''    
        print('list_loc_r = ', list_A)  
        print('list_loc_peak = ', list_B)
        print('list_loc_nearest = ', list_loc_nearest)
        '''
    return np.asarray(list_loc_nearest)

def get_nearest_max(list_A, list_B, sampling_b, list_Y, th): 
    list_loc_nearest = []
    for loc in list_A:
        '''
        near_list = list(x for x in list_B if abs(x-loc) < th)
        # height_list = list_Y[near_list]
        height_list =  list(list_Y[int(x)] for x in near_list)
        print('near list = ',  near_list)
        print('height = ', height_list)
        max_loc = max(near_list, key=lambda x:list_Y[int(time2index(x, sampling_b))])
        print('max_loc = ', max_loc)
        '''

        list_loc_nearest.append(max((x for x in list_B if abs(x-loc) <= th) ,
                                key=lambda x: list_Y[int(time2index(x, sampling_b))]))

    #print('list_loc_nearest = ', list_loc_nearest)
    #list_loc_nearest.append(min(list_B, key=lambda x:abs(x-loc)))
    #print('list_loc_nearest = ', list_loc_nearest)
    return np.asarray(list_loc_nearest)


def time2index(index_ms, sampling):
    return index_ms * sampling / 1000