如何在不使用多个 np 语句的情况下从某个百分位数及以上(例如:第 95-100 个百分位数的列表编号)打印列表中的值?

How do you print the values in a list from a certain percentile and up (ex: list no. from 95th-100rd percentile) without using multiple np statements?

我现在的代码是这样的:

    import numpy as np
    list1 = []
    n = int(input("Enter number of elements for list 1: "))
    for i in range(0, n):
        ele = float(input("Enter a number: "))
        list1.append(ele)
    list2 = []
    n = int(input("Enter number of elements for list 2: "))
    for i in range(0, n):
        ele = float(input("Enter a number: "))
        list2.append(ele)
    add = []
    add = list1 + list2
    print("\nThe new list is:",add)
    print("\n\n95th - 100rd percentiles of new list (in order):","\n",np.percentile(add, 95),"\n",np.percentile(add, 96),"\n",np.percentile(add, 97),"\n",np.percentile(add, 98),"\n",np.percentile(add, 99),"\n",np.percentile(add, 100))

基本上,我想要做的是在底部没有所有 np 语句的情况下获得相同的结果(有没有办法打印添加列表中从第 95 个到第 100 个百分位数的所有数字)?

非常感谢!

尝试这样的事情:

print("\n\n95th - 100rd percentiles of new list (in order):")
for i in range(95,101):
    print(np.percentile(add, i))

我认为没有 numpy 如果你想计算然后写一个函数来计算百分位值。

import math
def percentile_cal(lst, percentile):
  length = len(lst)
  return sorted(lst)[int(math.ceil((length * percentile) / 100)) - 1]

add = [1,2,3,4,5,6,7,8,9,10]
add.sort()
for i in range(95,101):
  res = percentile_cal(add, i)
  print(f"{i} percentile value is {res}")