<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>'pataprogramming &#187; Programming</title>
	<atom:link href="http://www.pataprogramming.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pataprogramming.com</link>
	<description>Curiosities of Unconventional Computing</description>
	<lastBuildDate>Sun, 14 Mar 2010 03:43:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Extract n largest values from a Python dict</title>
		<link>http://www.pataprogramming.com/2010/03/python-dict-n-largest/</link>
		<comments>http://www.pataprogramming.com/2010/03/python-dict-n-largest/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 05:49:17 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Planet PLUG]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[n-largest]]></category>
		<category><![CDATA[n-max]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pataprogramming.com/?p=54</guid>
		<description><![CDATA[Having stashed a bunch of data in a python dict, one often needs a quick way to extract the n largest.  Inconveniently for the particular problem I was working on, the key value for the dict was an object and the associated value was a floating-point weight.  A quick-and-dirty method is to use insert all [...]]]></description>
			<content:encoded><![CDATA[<p>Having stashed a bunch of data in a python <code>dict</code>, one often needs a quick way to extract the <em>n</em> largest.  Inconveniently for the particular problem I was working on, the key value for the <code>dict</code> was an object and the associated value was a floating-point weight.  A quick-and-dirty method is to use insert all the <code>dict</code>-keys into a heap, using the <code>dict</code>-value as the heap-key  Fortuitously, Python has both a heap module and convenient functions for using it in exactly this way.</p>
<pre>import heapq

def dict_nlargest(d,n):
    return heapq.nlargest(n ,d, key = lambda k: d[k])

d = { 'a':10.0, 'b':2.0, 'c':5.0, 'd':3.0, 'e':15.0 }
print dict_nlargest(d,3)
</pre>
<p>Which neatly prints a list of the keys associated with the three largest values:</p>
<pre>['e', 'a', 'c']
</pre>
<p>The <code>heapq.nlargest()</code> function can also use arbitrary object attributes as keys, as well, by supplying a one-argument function to the named parameter <code>key</code>.  (This is the role of the <code>lambda</code> in the examples above and below.)</p>
<pre>import heapq
class Toy:
    def __init__Toy(self, s, i, f):
        self.s = s
        self.i = i
        self.f = f

l = [ Toy('a', 10, 4.4), Toy('b', 4, 12.0),
      Toy('c', 2, 20.0), Toy('d', 1, 5.0) ]

# largest by the 'f' attribute
print heapq.nlargest(2, l, key = lambda o: o.f)

# largest by the 'i' attribute
print heapq.nlargest(2, l, key = lambda o: o.i)
</pre>
<p>This is fine for a one-off, but you&#8217;ll likely want to maintain this information in a data structure if this you&#8217;ll be performing this operation multiple times on a large collection.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pataprogramming.com/2010/03/python-dict-n-largest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

