fork download
  1. import numpy as np
  2.  
  3. def nearest( list, num ):
  4. return list[ np.abs(np.asarray(list) - num).argmin() ]
  5.  
  6. if __name__ == "__main__":
  7. list = [98 ,100 ,198 ,200 ,250 ,298]
  8. print(nearest(list, 50)) # →98
  9. print(nearest(list, 195)) # →198
  10.  
  11.  
Success #stdin #stdout 0.24s 27056KB
stdin
Standard input is empty
stdout
98
198