获取python中列表的百分比范围

获取python中列表的百分比范围,python,python-3.x,python-2.7,numpy,Python,Python 3.x,Python 2.7,Numpy,我试图在列表中找到给定百分比的数据范围。我希望我的结果是该范围的最高和最低极限。这是迄今为止我找到的最简单可行的解决方案。”字符串格式只是为了测试,当我实现它时,它将只返回所需的值'。如果您不理解我试图实现的目标,那么运行我的代码,您应该能够理解我试图实现的目标 #Sample Data List data = [ 4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193, -1.001, 11.

我试图在列表中找到给定百分比的数据范围。我希望我的结果是该范围的最高和最低极限。这是迄今为止我找到的最简单可行的解决方案。”字符串格式只是为了测试,当我实现它时,它将只返回所需的值'。如果您不理解我试图实现的目标,那么运行我的代码,您应该能够理解我试图实现的目标

#Sample Data List
data = [
    4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193,
    -1.001, 11.085, 1.335, 11.763, -7.284, -1.733, 3.175, 3.077, 0.249, -2.095,
    -0.253, 11.095, 5.184, 1.45, -3.882, 8.251, -4.234, -0.096, 5.957, -4.473,
    -3.874, 3.246, -0.929, -1.707, 2.079, 0.647, -8.189, 2.074, 3.226, 0.123,
    -3.097, -5.706, 4.276, 7.377, -5.861, 0.357, 0.966, -0.63, 2.129, 1.514,
    6.82, -1.854, 8.394, 5.141, -4.501, 1.307, -2.812, -0.348, 2.729, 2.232,
    4.096, -2.257, -4.802, -1.116, 1.346, 4.733, -1.391, -2.986, 2.8, 4.949,
    -4.101, 2.727, -1.267, -15.034, 5.322, 7.011, 6.36, 0.16, 0.719, 4.302,
    1.365, 3.098, -2.892, -4.589, -5.516, 2.182, -6.019, -6.75, 15.499, -4.58,
    3.516, 1.18, 9.648, -1.21, -10.911, -0.583, -0.545, 3.286, 0.51, 9.578,
    -2.444, 2.771, -3.846, -3.819, 1.877, 1.392, -2.784, -5.907, 4.206, 1.42,
    -2.279, -0.358, -0.649, 2.052, 1.04, 1.764, -3.156, -2.685, 0.106, 3.569,
    0.944, 3.797, -0.76, 4.89, -3.014, -2.118, -1.142, -1.578, -8.84, 3.034,
    -2.693, -2.989, -0.815, -2.504, 3.147, 1.596, -2.94, 0.906, -0.154, -3.765
     ]

#Input for percent range
percent = 50

#Get half of percentage
halfPercent = int(int((len(data) * float('.'+str(percent))/2)))

#Build string for printing later and create a counter
string = str(percent)+'% of data entries are beween values '
counter = 0

#Sort data and iterate up until it get to the counter gets to the halfPercent
for item in sorted(data):
    if item >= 0:
        counter += 1
        if counter >= halfPercent:
            string += str(item)
            counter = 0
            break
string += ' and '

#Sort data and iterate up until it get to the counter gets to the halfPercent
for item in sorted(data, reverse=True):
    if item <= 0:
        counter += 1
        if counter >= halfPercent:
            string += str(item)
            break

#Do the printing
print 'Min is ' + str(min(data)) + ' and Max is ' + str(max(data))
print string

我仍然想知道是否可以用更少的代码来完成。可能使用numpy或其他库?

此代码的输出与问题中代码的输出相同

data = [
4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193,
-1.001, 11.085, 1.335, 11.763, -7.284, -1.733, 3.175, 3.077, 0.249, -2.095,
-0.253, 11.095, 5.184, 1.45, -3.882, 8.251, -4.234, -0.096, 5.957, -4.473,
-3.874, 3.246, -0.929, -1.707, 2.079, 0.647, -8.189, 2.074, 3.226, 0.123,
-3.097, -5.706, 4.276, 7.377, -5.861, 0.357, 0.966, -0.63, 2.129, 1.514,
6.82, -1.854, 8.394, 5.141, -4.501, 1.307, -2.812, -0.348, 2.729, 2.232,
4.096, -2.257, -4.802, -1.116, 1.346, 4.733, -1.391, -2.986, 2.8, 4.949,
-4.101, 2.727, -1.267, -15.034, 5.322, 7.011, 6.36, 0.16, 0.719, 4.302,
1.365, 3.098, -2.892, -4.589, -5.516, 2.182, -6.019, -6.75, 15.499, -4.58,
3.516, 1.18, 9.648, -1.21, -10.911, -0.583, -0.545, 3.286, 0.51, 9.578,
-2.444, 2.771, -3.846, -3.819, 1.877, 1.392, -2.784, -5.907, 4.206, 1.42,
-2.279, -0.358, -0.649, 2.052, 1.04, 1.764, -3.156, -2.685, 0.106, 3.569,
0.944, 3.797, -0.76, 4.89, -3.014, -2.118, -1.142, -1.578, -8.84, 3.034,
-2.693, -2.989, -0.815, -2.504, 3.147, 1.596, -2.94, 0.906, -0.154, -3.765]

half_way = len(data) // 4  # // division returns an int
sorted_data = sorted(data)

print("""Min is {} and Max is {}
50% of data entries are between values {} and {}""".format(
    max(sorted_data),
    min(sorted_data),
    max([item for item in sorted_data if item >= 0][0:half_way]),
    min([item for item in reversed(sorted_data) if item <= 0][0:half_way]),
))  # an alternate way of reversing an array is sorted_data[::-1]
数据=[
4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193,
-1.001, 11.085, 1.335, 11.763, -7.284, -1.733, 3.175, 3.077, 0.249, -2.095,
-0.253, 11.095, 5.184, 1.45, -3.882, 8.251, -4.234, -0.096, 5.957, -4.473,
-3.874, 3.246, -0.929, -1.707, 2.079, 0.647, -8.189, 2.074, 3.226, 0.123,
-3.097, -5.706, 4.276, 7.377, -5.861, 0.357, 0.966, -0.63, 2.129, 1.514,
6.82, -1.854, 8.394, 5.141, -4.501, 1.307, -2.812, -0.348, 2.729, 2.232,
4.096, -2.257, -4.802, -1.116, 1.346, 4.733, -1.391, -2.986, 2.8, 4.949,
-4.101, 2.727, -1.267, -15.034, 5.322, 7.011, 6.36, 0.16, 0.719, 4.302,
1.365, 3.098, -2.892, -4.589, -5.516, 2.182, -6.019, -6.75, 15.499, -4.58,
3.516, 1.18, 9.648, -1.21, -10.911, -0.583, -0.545, 3.286, 0.51, 9.578,
-2.444, 2.771, -3.846, -3.819, 1.877, 1.392, -2.784, -5.907, 4.206, 1.42,
-2.279, -0.358, -0.649, 2.052, 1.04, 1.764, -3.156, -2.685, 0.106, 3.569,
0.944, 3.797, -0.76, 4.89, -3.014, -2.118, -1.142, -1.578, -8.84, 3.034,
-2.693, -2.989, -0.815, -2.504, 3.147, 1.596, -2.94, 0.906, -0.154, -3.765]
half_way=len(data)//4#//除法返回整数
已排序的\ u数据=已排序的(数据)
打印(““”最小值为{},最大值为{}”
50%的数据项在值{}和{}“.”之间(
最大值(已排序的数据),
最小值(已排序的_数据),
最大值([如果项>=0,则排序数据中的项对应于项][0:中间值],

最小值([反向(排序的_数据)中项目的项目)如果项目“在列表中查找给定百分比的数据范围”确切的意思是什么?你能举个例子吗?请不要让变量名为
list
,它只是隐藏了内置函数
list
。这只是我试图实现的一个例子。当我用数据库中的列表数据集实现这个值时。我编辑了我的帖子,将列表更改为数据。@ReblochonMasque如果你t运行代码输出应该可以解释您的问题。用更少的代码来实现令人满意的结果。我希望numpy或其他python库具有类似的功能。我可以在处理金融市场时大量使用。请注意,我的百分比是一个变量,因此可以在以后动态更改。如果满足,请接受答案你的要求。
Max is 15.499, Min is -15.034 and 90% of data entries are between values -8.84 and 6.36
data = [
4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193,
-1.001, 11.085, 1.335, 11.763, -7.284, -1.733, 3.175, 3.077, 0.249, -2.095,
-0.253, 11.095, 5.184, 1.45, -3.882, 8.251, -4.234, -0.096, 5.957, -4.473,
-3.874, 3.246, -0.929, -1.707, 2.079, 0.647, -8.189, 2.074, 3.226, 0.123,
-3.097, -5.706, 4.276, 7.377, -5.861, 0.357, 0.966, -0.63, 2.129, 1.514,
6.82, -1.854, 8.394, 5.141, -4.501, 1.307, -2.812, -0.348, 2.729, 2.232,
4.096, -2.257, -4.802, -1.116, 1.346, 4.733, -1.391, -2.986, 2.8, 4.949,
-4.101, 2.727, -1.267, -15.034, 5.322, 7.011, 6.36, 0.16, 0.719, 4.302,
1.365, 3.098, -2.892, -4.589, -5.516, 2.182, -6.019, -6.75, 15.499, -4.58,
3.516, 1.18, 9.648, -1.21, -10.911, -0.583, -0.545, 3.286, 0.51, 9.578,
-2.444, 2.771, -3.846, -3.819, 1.877, 1.392, -2.784, -5.907, 4.206, 1.42,
-2.279, -0.358, -0.649, 2.052, 1.04, 1.764, -3.156, -2.685, 0.106, 3.569,
0.944, 3.797, -0.76, 4.89, -3.014, -2.118, -1.142, -1.578, -8.84, 3.034,
-2.693, -2.989, -0.815, -2.504, 3.147, 1.596, -2.94, 0.906, -0.154, -3.765]

half_way = len(data) // 4  # // division returns an int
sorted_data = sorted(data)

print("""Min is {} and Max is {}
50% of data entries are between values {} and {}""".format(
    max(sorted_data),
    min(sorted_data),
    max([item for item in sorted_data if item >= 0][0:half_way]),
    min([item for item in reversed(sorted_data) if item <= 0][0:half_way]),
))  # an alternate way of reversing an array is sorted_data[::-1]