Search code examples
pythonnumpyquartile

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?


My current code is this:

    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))

Basically, what I want to do is get the same result without all the np statements at a bottom (is there a way to print all the numbers in the add list from the 95th to 100rd percentile)?

Thank you very much!


Solution

  • Try something like this:

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