如何从1000个数中选出top10?

使用Python和分治法

 

from random import random
target = [random() for i in range(10000)]

def topn(target, n):
    if len(target) <= n:
        return target
    else:
        part1 = target[0:len(target)/2]
        part2 = target[len(target)/2:]
        top2n = (topn(part1, n) + topn(part2, n))
        top2n.sort()
        return top2n[0:n]

if __name__ == '__main__':
    top10 = topn(target, 10)
 

你可能感兴趣的:(python)