<?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>Autarchy of the Private Cave &#187; sorting</title> <atom:link href="https://bogdan.org.ua/tags/sorting/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>How to sort Python dict (dictionary)</title><link>https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html</link> <comments>https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html#comments</comments> <pubDate>Wed, 26 Sep 2007 06:20:57 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[example]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[sorting]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html</guid> <description><![CDATA[Sample script (copypasted from Well House Consultants training course): click the PLAIN TEXT header for copy-pasteable version #!/usr/local/bin/python &#160; author = &#123;&#34;php&#34;:&#34;Rasmus Lerdorf&#34;,\ &#160; &#160; &#34;perl&#34;:&#34;Larry Wall&#34;,\ &#160; &#160; &#34;tcl&#34;:&#34;John Ousterhout&#34;,\ &#160; &#160; &#34;awk&#34;:&#34;Brian Kernighan&#34;,\ &#160; &#160; &#34;java&#34;:&#34;James Gosling&#34;,\ &#160; &#160; &#34;parrot&#34;:&#34;Simon Cozens&#34;,\ &#160; &#160; &#34;python&#34;:&#34;Guido van Rossum&#34;&#125; &#160; langs = author.keys&#40;&#41; langs.sort&#40;&#41; &#160; for [&#8230;]]]></description> <content:encoded><![CDATA[<p>Sample script (copypasted from <a
href="http://www.wellho.net/resources/ex.php4?item=y107/d3.py">Well House Consultants training course</a>):<br
/> <em>click the PLAIN TEXT header for copy-pasteable version</em></p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #808080; font-style: italic;">#!/usr/local/bin/python</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">author <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">&quot;php&quot;</span>:<span
style="color: #483d8b;">&quot;Rasmus Lerdorf&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;perl&quot;</span>:<span
style="color: #483d8b;">&quot;Larry Wall&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;tcl&quot;</span>:<span
style="color: #483d8b;">&quot;John Ousterhout&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;awk&quot;</span>:<span
style="color: #483d8b;">&quot;Brian Kernighan&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;java&quot;</span>:<span
style="color: #483d8b;">&quot;James Gosling&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;parrot&quot;</span>:<span
style="color: #483d8b;">&quot;Simon Cozens&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;python&quot;</span>:<span
style="color: #483d8b;">&quot;Guido van Rossum&quot;</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">langs <span
style="color: #66cc66;">=</span> author.<span
style="color: black;">keys</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">langs.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> language <span
style="color: #ff7700;font-weight:bold;">in</span> langs:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> language<span
style="color: #66cc66;">,</span><span
style="color: #483d8b;">&quot;is the child of&quot;</span><span
style="color: #66cc66;">,</span>author<span
style="color: black;">&#91;</span>language<span
style="color: black;">&#93;</span></div></li></ol></div></div><p>You can also define the Python <strong>ksort()</strong> function similar to that found in PHP:<br
/> <span
id="more-228"></span></p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">def</span> ksort<span
style="color: black;">&#40;</span>d<span
style="color: #66cc66;">,</span> func <span
style="color: #66cc66;">=</span> <span
style="color: #008000;">None</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; keys <span
style="color: #66cc66;">=</span> d.<span
style="color: black;">keys</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; keys.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span>func<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> keys</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #808080; font-style: italic;"># example use, assume 'd' is a dictionary</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> k <span
style="color: #ff7700;font-weight:bold;">in</span> ksort<span
style="color: black;">&#40;</span>d<span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k<span
style="color: #66cc66;">,</span> d<span
style="color: black;">&#91;</span>k<span
style="color: black;">&#93;</span></div></li></ol></div></div><p>(example taken from <a
href="http://bytes.com/topic/python/answers/21704-sort-dictionary">TheScript forum</a>)</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&#038;title=How%20to%20sort%20Python%20dict%20%28dictionary%29" data-a2a-url="https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html" data-a2a-title="How to sort Python dict (dictionary)"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>