Finding k closest numbers to a given number

The short answer The heapq.nsmallest() function will do this neatly and efficiently: >>> from heapq import nsmallest >>> s = [1,2,3,4,5,6,7] >>> nsmallest(3, s, key=lambda x: abs(x – 6.5)) [6, 7, 5] Essentially this says, “Give me the three input values that have the smallest absolute difference from the number 6.5“. Optimizing for repeated lookups … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)