1

我目前的代码是这样的:

    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 个百分位的所有数字)?

非常感谢!

4

2 回答 2

0

尝试这样的事情:

print("\n\n95th - 100rd percentiles of new list (in order):")
for i in range(95,101):
    print(np.percentile(add, i))
于 2021-07-29T06:48:12.523 回答
0

如果你想计算,我认为没有 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}")
于 2021-07-29T07:43:12.450 回答