如何从数字列表中获得平均“差价”?

问题描述 投票:5回答:2

让我们说我有一个数字列表:

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]

但是这个列表包含了太多的数据,我想传达这种传播。我想打印其中10个数字。如果1是最高数字而10是最低数字,我怎么能对列表进行排序并打印10个数字的范围来表示从最高到最低的差价?

让我们说列表是[1,2,3,4,5,6,7,8,9,10,11,12,13],我希望在该范围内传播四个数字,传播将是[1,5,9,13]

python
2个回答
9
投票

试试这个:

sorted(list)[::len(list)/9]

输出:

[6.34, 11.19, 13.61, 14.56, 15.92, 16.91, 17.97, 19.65, 20.87, 26.81]

编辑:

qazxsw poi不会出现在qazxsw poi的名单上


7
投票

使用max(list)len(list) % 9 != 0

np.percentile

逻辑:

np.linspaceimport 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]) 返回给定数组的最近np.percentile-th元素。

interpolation='nearest'创建等间距的10个元素,从0和100(包括0和100),以产生第q个百分位数。

© www.soinside.com 2019 - 2024. All rights reserved.