简体   繁体   中英

How to find k biggest numbers from a list of n numbers assuming n > k

I am looking for some code in Python which could return k largest numbers from an unsorted list of n numbers. First I thought to do this by sorting the list first, but this might turn out to be very bulky.

For example the list from which I want to find k largest number be list1

> list1 = [0.5, 0.7, 0.3, 0.3, 0.3, 0.4, 0.5]

Here n = 7 and if k = 3 , that is if I want to find 3 largest numbers from a list of 7 numbers then output should be 0.5, 0.7, 0.5

How can this be done?

Python has all batteries included - use heapq module :)

from heapq import nlargest

data = [0.5, 0.7, 0.3, 0.3, 0.3, 0.4, 0.5]
print nlargest(3, data)

it's also faster than sorting the whole array, because it's using partial heapsort

It can be done like this:

>>> list1
[0.5, 0.7, 0.3, 0.3, 0.3, 0.4, 0.5]
>>> list2 = list1[:] #make a copy of list1
>>> k = 3
>>> result = []
>>> for i in range(k):
        result.append(max(list2)) #append largest element to list of results
        list2.remove(max(list2)) # remove largest element from old list
>>> result
[0.7, 0.5, 0.5]
>>> 

Assuming you don't want to modify list1 , you make a sorted copy:

In [1]: list1 = [0.5, 0.7, 0.3, 0.3, 0.3, 0.4, 0.5]

In [2]: list2 = sorted(list1)

In [3]: list2
Out[3]: [0.3, 0.3, 0.3, 0.4, 0.5, 0.5, 0.7]

In list2 , the largest numbers are the last numbers, so we'll use slicing :

In [4]: list2[-3:]
Out[4]: [0.5, 0.5, 0.7]

The links I've added point to Pythons documentation . As a beginner, you should start with having a look at the tutorial . After that, the library reference is what you'll be needing most, because the large standard library is one of the things that makes python so nice.

Another solution:

import numpy as np
k = 3
ind = np.argpartition(list1, -k)[-k:] # index of the k highest elements
print(list1[ind])

without sorting, using a greedy approach

k_largest = list1[:k]
for num in list1:
    for c in range(k):
        if num > k_largest[c]:
            k_largest = k_largest[:c] + [num] + k_largest[c:-1]  # maintains sorted order
            break
print(k_largest)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM