Last active
August 24, 2016 08:45
-
-
Save icchi-h/1d0bb1c52ebfdd31f14b3e811328390a to your computer and use it in GitHub Desktop.
Pythonのリスト要素からある値と最も近い値を取り出す ref: http://qiita.com/icchi_h/items/fc0df3abb02b51f81657
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import numpy as np | |
def getNearestValue(list, num): | |
""" | |
概要: リストからある値に最も近い値を返却する関数 | |
@param list: データ配列 | |
@param num: 対象値 | |
@return 対象値に最も近い値 | |
""" | |
# リスト要素と対象値の差分を計算し最小値のインデックスを取得 | |
idx = np.abs(np.asarray(list) - num).argmin() | |
return list[idx] | |
if __name__ == "__main__": | |
list = [0, 0.5, 1] | |
print(getNearestValue(list, -0.1)) # →0 | |
print(getNearestValue(list, 0.1)) # →0 | |
print(getNearestValue(list, 0.4)) # →0.5 | |
print(getNearestValue(list, 0.5)) # →0.5 | |
print(getNearestValue(list, 0.6)) # →0.5 | |
print(getNearestValue(list, 0.8)) # →1 | |
print(getNearestValue(list, 1.2)) # →1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import numpy as np | |
def getNearestValue(list, num): | |
""" | |
概要: リストからある値に最も近い値を返却する関数 | |
@param list: データ配列 | |
@param num: 対象値 | |
@return 対象値に最も近い値 | |
""" | |
list = list.sort | |
# リスト範囲外の場合の処理 | |
if num < min(list): | |
return list[0] | |
elif num > max(list): | |
return list[-1] | |
# 昇順に挿入する際のインデックスを取得 | |
sortIdx = np.searchsorted(list, num, side='left') | |
# 周辺の値をリスト化 | |
sideValueList = list[sortIdx-1:sortIdx+2] | |
# print(sideValueList) | |
# 周辺の値と求める値の差分をリスト化 | |
diffList = [(np.abs(val-num)) for val in sideValueList] | |
# print(diffList) | |
# 差分リストの最小値から入力リストから最も近い値のインデックスを計算 | |
nearestIdx = (sortIdx-1) + diffList.index(min(diffList)) | |
return list[nearestIdx] | |
if __name__ == "__main__": | |
list = [0, 0.5, 1] | |
print(getNearestValue(list, -0.1)) # →0 | |
print(getNearestValue(list, 0.1)) # →0 | |
print(getNearestValue(list, 0.4)) # →0.5 | |
print(getNearestValue(list, 0.5)) # →0.5 | |
print(getNearestValue(list, 0.6)) # →0.5 | |
print(getNearestValue(list, 0.8)) # →1 | |
print(getNearestValue(list, 1.2)) # →1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import numpy as np | |
def getNearestValue(list, num): | |
# 昇順に挿入する際のインデックスを取得 | |
sortIdx = np.searchsorted(list, num, side='left') | |
return list[sortIdx] | |
if __name__ == "__main__": | |
list = [0, 0.5, 1] | |
print(getNearestValue(list, 0.1)) # →0 OK | |
print(getNearestValue(list, 0.4)) # →0 NG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment