Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an average 'spread' from a list of numbers?

Tags:

python

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].

like image 479
Zak Avatar asked Feb 22 '19 09:02

Zak


1 Answers

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

like image 75
Carles Mitjans Avatar answered Oct 04 '22 12:10

Carles Mitjans