Skip to content
Advertisement

Tag: closest

Finding k closest numbers to a given number

Say I have a list [1,2,3,4,5,6,7]. I want to find the 3 closest numbers to, say, 6.5. Then the returned value would be [5,6,7]. Finding one closest number is not that tricky in python, which can be done using But I am trying not to put a loop around this to find k closest numbers. Is there a pythonic way

Advertisement