2014年3月11日 星期二

006 Python Cookbook 01-04 Finding the Largest N

#!/usr/bin/env python

# http://chimera.labs.oreilly.com/books/1230000000393/ch01.html#findingthelargestorsmallest
# Finding the Largest or Smallest N Items


import heapq

def do_sth_on_list_num(inlist):
    print "inlist = ", inlist
    print "heapq.nlargest(3, inlist)  = ", heapq.nlargest(3, inlist)
    print "heapq.nsmallest(3, inlist) = ", heapq.nsmallest(3, inlist)
    print

def do_sth_on_list_nested(llist):
    print "llist = "
    for i in llist:
        print "\t", i
    print
    lexpensive = heapq.nlargest(1, llist, key=lambda s: s['price'] )
    print "Most expensive = ", lexpensive
    lpopular = heapq.nsmallest(1, llist, key=lambda s: s['userbase'] )
    print "Less popular   = ", lpopular
    print

def do_sth_for_top2(llist):
    print "llist = "
    for i in llist:
        print "\t", i
    print
    print "Most expensive = "
    lexpensive = heapq.nlargest(2, llist, key=lambda s: s['price'] )
    for j in lexpensive:
        print "\t", j
    print "Less popular   = "
    lpopular = heapq.nsmallest(2, llist, key=lambda s: s['userbase'] )  
    for k in lpopular:
        print "\t", k
    print

def do_sth_on_list_by_heap(inlist):
    print "inlist = ", inlist
    heap = list(inlist)
    print "after heapq.heapify(heap)"
    heapq.heapify(heap)
    while len(heap) > 0:
        print "\theapq.heappop(heap)", heapq.heappop(heap)
    print

print "----------"
print "do_sth_on_list_num(list_num)"
list_num = [2, 4, 6, 8, 7, 9, 5, 3, 1]
do_sth_on_list_num(list_num)

print "----------"
print "do_sth_on_list_nested(list_list)"
list_list = [
    {'name': 'line', 'userbase': 100, 'price': 0 },
    {'name': 'whatsapp', 'userbase': 150, 'price': 1.0 },
    {'name': 'skype', 'userbase': 500, 'price': 0.99 }
]
do_sth_on_list_nested(list_list)

print "----------"
print "do_sth_for_top2(list_list)"
do_sth_for_top2(list_list)

print "----------"
print "do_sth_on_list_by_heap(list_num)"
do_sth_on_list_by_heap(list_num)

----------
do_sth_on_list_num(list_num)
inlist =  [2, 4, 6, 8, 7, 9, 5, 3, 1]
heapq.nlargest(3, inlist)  =  [9, 8, 7]
heapq.nsmallest(3, inlist) =  [1, 2, 3]

----------
do_sth_on_list_nested(list_list)
llist =
{'price': 0, 'userbase': 100, 'name': 'line'}
{'price': 1.0, 'userbase': 150, 'name': 'whatsapp'}
{'price': 0.99, 'userbase': 500, 'name': 'skype'}

Most expensive =  [{'price': 1.0, 'userbase': 150, 'name': 'whatsapp'}]
Less popular   =  [{'price': 0, 'userbase': 100, 'name': 'line'}]

----------
do_sth_for_top2(list_list)
llist =
{'price': 0, 'userbase': 100, 'name': 'line'}
{'price': 1.0, 'userbase': 150, 'name': 'whatsapp'}
{'price': 0.99, 'userbase': 500, 'name': 'skype'}

Most expensive =
{'price': 1.0, 'userbase': 150, 'name': 'whatsapp'}
{'price': 0.99, 'userbase': 500, 'name': 'skype'}
Less popular   =
{'price': 0, 'userbase': 100, 'name': 'line'}
{'price': 1.0, 'userbase': 150, 'name': 'whatsapp'}

----------
do_sth_on_list_by_heap(list_num)
inlist =  [2, 4, 6, 8, 7, 9, 5, 3, 1]
after heapq.heapify(heap)
heapq.heappop(heap) 1
heapq.heappop(heap) 2
heapq.heappop(heap) 3
heapq.heappop(heap) 4
heapq.heappop(heap) 5
heapq.heappop(heap) 6
heapq.heappop(heap) 7
heapq.heappop(heap) 8
heapq.heappop(heap) 9

沒有留言:

張貼留言