I want to get 5 integers closest to 100, such as 100, 99,98, 101, 102 I have used the following methods, but have not got the desired result.
myList = [95,96,97,98,99,100,101,102,103,104,105]
for i in myList:
print(min(myList, key=lambda x:abs(x-100)))
myList.remove(i)
output:
100
100
100
100
100
100
Then I did this:
myList = [95,96,97,98,99,100,101,102,103,104,105]
for i in myList:
print(min(myList, key=lambda x:abs(x-i)))
myList.remove(i)
output:
95
97
99
101
103
105
In this case, more than 95, 97, and 105; the 98, 96 and 102 are closest; but instead, these closes numbers are skipped.
Please, have a look and come up with suggestions. Thanks :)