get the 3 largest numbers from a python list

Asked

Viewed 2,710 times

1

someone can help me find the mistake?

"""Returns a list of the three largest Elements input_list in order from largest to smallest. If input_list has Fewer than three Elements, Return input_list element Sorted largest to smallest/"""

    # TODO: implement this function

    def top_three(input_list):
        list1 = [2,3,5,6,8,4,2,1]
        input_list = sorted(list1, reverse=True)
        return input_list[:3]
    print (top_three(1))
  • Why is there this [:2] function call? You should not pass the full list as parameter?

  • was to return the first three numbers. I did a new function but don’t know where the error is: def top_three(input_list): list1 = [2,3,5,6,8,4,2,1] input_list = Sorted(list1, Reverse=True) Return input_list[:3] print (top_three(1))

3 answers

3

You have two problems there. One of them is precisely that you are limiting the data of the list twice: when calling the function, you must pass its complete list, out of order -and it will return the 3 highest values. When writing the call the function as top_three(input_list[:2])) you are passing only the first three values of your list in the order in which they are. The function will "see" a sub-ist with only these three values,e , obviously, say that they are the three largest.

Now another problem there is that you’re not actually using the value passed to the function - in the first line within the function top_three you do input_list = [2,3,5,6,8,4,2,1]- and this over-writes the list passed as parameter to the function - and it will function always with the same numbers.

  • Thanks @jsbueno, I made the adjustments but I’m still having problems with because I can’t get the print to return the top 3 numbers now. def top_three(input_list): list1 = [2,3,5,6,8,4,2,1] Return Sorted(list1, Reverse=True)[input_list] print (top_three[:2])

  • Thank you very much for your help. I got the answer by turning around: def top_three(input_list): input_list.Sort() tops=input_list[-3:] q=Sorted(tops, Reverse=True) Return q list1 = top_three([[2,3,5,6,8,4,2,1]) print (list1)

2


Heapq

An alternative would be the use of heapq:

import heapq  
lst = [2, 3, 5, 6, 8, 4, 2, 1]

# Os 3 maiores:
heapq.nlargest(3,lst)
[8, 6, 5]

# Os 3 menores
heapq.nsmallest(3,lst)
[1, 2, 2]
  • Thanks Sidon, but I have to use the function for the object in question.

  • Ok, just use heapq inside the function, but I will edit the answer and put an alternative if you can’t use heapq. Stay Tuned. :-)

  • Actually, I was already in the question. Forget it.

  • I managed with the code below, I will search for your solution that seems to be better than the function for this type of parameter. def top_three(input_list): input_list.Sort() tops=input_list[-3:] q=Sorted(tops, Reverse=True) Return q list1 = top_three([2,3,5,6,4,2,1]) print (list1)

0

It worked that way:

def top_three(input_list):
    input_list.sort()
    tops=input_list[-3:]
    q=sorted(tops, reverse=True)
    return q

list1 = top_three([2,3,5,6,8,4,2,1])
print (list1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.