Lets say I have a list of numbers:
some_numbers = [16.0, 16.01, 24.53, 22.99, 22.72, 22.71, 22.2, 21.36, 21.34,
21.0, 22.67, 22.62, 15.89, 23.54, 27.0, 21.35, 26.99, 25.46, 22.54, 22.53,
17.99, 22.13, 17.97, 17.96, 17.95, 22.4, 22.32, 22.25, 22.19, 22.16,
20.68, 21.74, 15.38, 11.13, 15.82, 22.33, 22.31, 22.23, 22.15, 22.12,
22.11, 22.07, 18.99, 18.94, 18.86, 18.85, 18.82, 18.81, 16.79, 15.98,
15.96, 15.94, 15.9, 15.86, 15.85, 15.83, 11.47, 11.46, 11.36, 11.34,
11.32, 11.28, 11.26, 11.25, 11.21, 11.19, 11.18, 9.99]
but the list contains a little too much data, and I want to convey the spread. I want to print 10 of those numbers. With 1 being the highest number and 10 being the lowest number, how could I sort the list and print a range of 10 numbers to represent the spread from highest to lowest?
Lets say the list is [1,2,3,4,5,6,7,8,9,10,11,12,13]
and I wanted a spread of four numbers in that range, the spread would be [1,5,9,13]
.
Try this:
sorted(list)[::len(list)/9]
Output:
[6.34, 11.19, 13.61, 14.56, 15.92, 16.91, 17.97, 19.65, 20.87, 26.81]
Edit:
max(list)
won't be on this list if len(list) % 9 != 0
Using np.percentile
and np.linspace
:
import numpy as np
np.percentile(l, np.linspace(100, 0, 10), interpolation='nearest')
# array([27. , 21.02, 19.7 , 17.99, 16.93, 15.94, 14.57, 13.77, 11.19,
6.34])
Logic:
np.percentile
with interpolation='nearest'
returns nearest q
-th element of given array.
np.linspace
creates equally spaced 10 elements from 0 and 100 inclusive, in order to make q-th percentiles.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.