Key:
min(list_B, key=lambda x:abs(x-mydata) # 從 list_B 傳回 與 mydata 最接近的數字
作法
def test_get_nearest():
list_A = [10, 20, 30, 40]
list_B = [11, 22, 33, 44]
get_nearest(list_A, list_B) # output: [11, 22, 33, 44]
# 從 list B 中, 找出最接近 list A 的數值串列
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_nearest = ', list_loc_nearest)
Reference