<?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 Caveexample &#187;</title> <atom:link href="http://bogdan.org.ua/tags/example/feed" rel="self" type="application/rss+xml" /><link>http://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Tue, 15 May 2012 21:56:55 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>C: how to specify comparison operators floating precision</title><link>http://bogdan.org.ua/2009/06/11/c-how-to-specify-comparison-operators-floating-precision.html</link> <comments>http://bogdan.org.ua/2009/06/11/c-how-to-specify-comparison-operators-floating-precision.html#comments</comments> <pubDate>Thu, 11 Jun 2009 17:47:37 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[how-to]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[C]]></category> <category><![CDATA[comparison]]></category> <category><![CDATA[double]]></category> <category><![CDATA[example]]></category> <category><![CDATA[float]]></category> <category><![CDATA[floating]]></category> <category><![CDATA[operator]]></category> <category><![CDATA[precision]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=774</guid> <description><![CDATA[There is no way I'm aware of to do what the title says. However... I'm sure that you are aware of the fact that floats representation in any programming language is limited by the precision of the internal binary representations. In other words, you can never have an exact float representation - there will always [...]]]></description> <content:encoded><![CDATA[<p>There is no way I'm aware of to do what the title says. However...</p><p>I'm sure that you are aware of the fact that floats representation in any programming language is limited by the precision of the internal binary representations. In other words, you can never have an <strong>exact</strong> float representation - there will always be some precision associated with the float you are working with. The simplest example is the difference in precision between the <em>float</em> and <em>double</em> types in <strong>C</strong>.</p><p>Suppose I have the following code fragment:</p><div
class="igBar"><span
id="lc-4"><a
href="#" onclick="javascript:showPlainTxt('c-4'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C:</span><div
id="c-4"><div
class="c"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #b1b100;">if</span> <span
style="color: #66cc66;">&#40;</span> result.<span
style="color: #202020;">score</span> &gt;= input-&gt;raw_cut_off <span
style="color: #66cc66;">&#41;</span></div></li></ol></div></div></div><p></p><p>Both <em>result.score</em> and <em>input->raw_cut_off</em> are of type <strong>float</strong>, and can have positive and negative values. When compared with the greater than or equal ( >= ) operator, it is not always that condition is true - for the precision reasons shortly mentioned above.</p><p>As I already said, there is no precision specification for equality operators in <strong>C</strong>. But it is quite simple to "invent" precision specification; e.g. if I wanted to test for equality only, I could write</p><div
class="igBar"><span
id="lc-5"><a
href="#" onclick="javascript:showPlainTxt('c-5'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C:</span><div
id="c-5"><div
class="c"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #b1b100;">if</span> <span
style="color: #66cc66;">&#40;</span> fabsf<span
style="color: #66cc66;">&#40;</span> result.<span
style="color: #202020;">score</span> - input-&gt;raw_cut_off <span
style="color: #66cc66;">&#41;</span> &lt; <span
style="color: #cc66cc;color:#800000;">0</span>.<span
style="color: #cc66cc;color:#800000;">000001</span> <span
style="color: #66cc66;">&#41;</span></div></li></ol></div></div></div><p></p><p>In this example, I'm effectively asking for 6-digit precision for the equality comparison of floating-point values. Note, that if you replace that 0.000001 with the actual precision limit of the floating type you are using, you will be "exactly" comparing floating-point numbers - up to that type's precision, of course <img
src='http://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p><p>The first-most example with the >= operator can be rewritten as</p><div
class="igBar"><span
id="lc-6"><a
href="#" onclick="javascript:showPlainTxt('c-6'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C:</span><div
id="c-6"><div
class="c"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #b1b100;">if</span> <span
style="color: #66cc66;">&#40;</span> result.<span
style="color: #202020;">score</span> &gt; <span
style="color: #66cc66;">&#40;</span> input-&gt;raw_cut_off - precision<span
style="color: #66cc66;">&#41;</span> <span
style="color: #66cc66;">&#41;</span></div></li></ol></div></div></div><p> where <em>precision</em> is exactly what it is named, e.g. <em>precision</em> = 0.000001.</p><p>Sources used:</p><ul><li>comment by <a
href="http://bytes.com/topic/c-sharp/answers/233215-float-double-arithmetic-precision-error#post953273">Randy A. Ynchausti</a></li><li><a
href="http://www.shokhirev.com/nikolai/abc/sciprog/sciprognum.html">scientific programming</a></li></ul><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2009/06/11/c-how-to-specify-comparison-operators-floating-precision.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2009/06/11/c-how-to-specify-comparison-operators-floating-precision.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F11%2Fc-how-to-specify-comparison-operators-floating-precision.html&amp;title=C%3A%20how%20to%20specify%20comparison%20operators%20floating%20precision" id="wpa2a_4"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2009/06/11/c-how-to-specify-comparison-operators-floating-precision.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>How to use ffmpeg if libmp3lame support is not present</title><link>http://bogdan.org.ua/2008/04/12/how-to-use-ffmpeg-if-libmp3lame-support-is-not-present.html</link> <comments>http://bogdan.org.ua/2008/04/12/how-to-use-ffmpeg-if-libmp3lame-support-is-not-present.html#comments</comments> <pubDate>Sat, 12 Apr 2008 20:41:07 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Software]]></category> <category><![CDATA[direct stream copy]]></category> <category><![CDATA[example]]></category> <category><![CDATA[ffmpeg]]></category> <category><![CDATA[libmp3lame]]></category> <category><![CDATA[sample command]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=291</guid> <description><![CDATA[I'm providing regularly updated compiled linux and freebsd ffmpeg binaries, and also described how to use ffmpeg on shared hostings if not all the required libraries (like libmp3lame) are present. However, the solution recommended might not "fit all", so here is another one - simpler and even more portable/universal than setting LD_LIBRARY_PATH. The solution is [...]]]></description> <content:encoded><![CDATA[<p>I'm providing <a
href="http://bogdan.org.ua/2007/06/28/compiled-linux-ffmpeg-binary-for-gallery2-download.html">regularly updated compiled linux and freebsd ffmpeg binaries</a>, and also <a
href="http://bogdan.org.ua/2008/03/12/instructions-on-installing-libmp3lame-enabled-ffmpeg-on-shared-linux-hosting.html">described how to use ffmpeg on shared hostings if not all the required libraries (like libmp3lame) are present</a>. However, the solution recommended might not "fit all", so here is another one - simpler and even more portable/universal than setting LD_LIBRARY_PATH.<br
/> <span
id="more-291"></span><br
/> The solution is simple - I first proposed it in the <a
href="http://drupal.org/node/137203#comment-802272">Getting ffmpeg working with flashvideo on various platforms</a> issue page on <a
href="http://drupal.org/">Drupal.org</a>.</p><p>The idea is: if the original audio stream in the file is already compressed (most of the time it is), then you do not need to recompress it - and do not need libmp3lame or any other audio codec! To achieve the "ffmpeg direct stream copy" functionality, you can add one of the two options:<br
/> <strong>-vcodec copy</strong> will direct-stream-copy video (no re-compression),<br
/> <strong>-acodec copy</strong> will direct-stream-copy audio (no re-compression).</p><p>In other words, ffmpeg option <strong>-acodec copy</strong> should just copy the original audio from input file to output file without alteration, and will not require any audio codecs support.</p><p>Sample command line could be (in the example, a 1-minute fragment of the input file is just copied into the output file):</p><p><strong>ffmpeg -t 00:01:00 -i movie.avi -vcodec copy -acodec copy preview.avi</strong></p><p>This tip is based on the information initially obtained from the ffmpeg-user mailing list archive for June of 2005.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2008/04/12/how-to-use-ffmpeg-if-libmp3lame-support-is-not-present.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2008/04/12/how-to-use-ffmpeg-if-libmp3lame-support-is-not-present.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2008%2F04%2F12%2Fhow-to-use-ffmpeg-if-libmp3lame-support-is-not-present.html&amp;title=How%20to%20use%20ffmpeg%20if%20libmp3lame%20support%20is%20not%20present" id="wpa2a_8"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2008/04/12/how-to-use-ffmpeg-if-libmp3lame-support-is-not-present.html/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</link> <comments>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comments</comments> <pubDate>Thu, 18 Oct 2007 13:20:21 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[database]]></category> <category><![CDATA[example]]></category> <category><![CDATA[examples]]></category> <category><![CDATA[exists]]></category> <category><![CDATA[insert]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[problem]]></category> <category><![CDATA[syntax]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</guid> <description><![CDATA[To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality. There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT ... ON DUPLICATE KEY UPDATE. Imagine we have a table: PLAIN TEXT SQL: CREATE [...]]]></description> <content:encoded><![CDATA[<p>To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality.</p><p>There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT ... ON DUPLICATE KEY UPDATE.</p><p>Imagine we have a table:</p><div
class="igBar"><span
id="lsql-10"><a
href="#" onclick="javascript:showPlainTxt('sql-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">SQL:</span><div
id="sql-10"><div
class="sql"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #993333; font-weight: bold;">CREATE</span> <span
style="color: #993333; font-weight: bold;">TABLE</span> <span
style="color: #ff0000;">`transcripts`</span> <span
style="color:#006600; font-weight:bold;">&#40;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color: #ff0000;">`ensembl_transcript_id`</span> varchar<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color: #cc66cc;color:#800000;">20</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color: #993333; font-weight: bold;">NOT</span> <span
style="color: #993333; font-weight: bold;">NULL</span>,</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color: #ff0000;">`transcript_chrom_start`</span> int<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color: #cc66cc;color:#800000;">10</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color: #993333; font-weight: bold;">UNSIGNED</span> <span
style="color: #993333; font-weight: bold;">NOT</span> <span
style="color: #993333; font-weight: bold;">NULL</span>,</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color: #ff0000;">`transcript_chrom_end`</span> int<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color: #cc66cc;color:#800000;">10</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color: #993333; font-weight: bold;">UNSIGNED</span> <span
style="color: #993333; font-weight: bold;">NOT</span> <span
style="color: #993333; font-weight: bold;">NULL</span>,</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color: #993333; font-weight: bold;">PRIMARY</span> <span
style="color: #993333; font-weight: bold;">KEY</span>&nbsp; <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color: #ff0000;">`ensembl_transcript_id`</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#41;</span> ENGINE=InnoDB <span
style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET=latin1;</div></li></ol></div></div></div><p></p><p>Now imagine that we have an automatic pipeline importing transcripts meta-data from Ensembl, and that due to various reasons the pipeline might be broken at any step of execution. Thus, we need to ensure two things: 1) repeated executions of the pipeline will not destroy our database, and 2) repeated executions will not die due to 'duplicate primary key' errors.</p><p>Method 1: using REPLACE<br
/> <span
id="more-238"></span><br
/> It's very simple:</p><div
class="igBar"><span
id="lsql-11"><a
href="#" onclick="javascript:showPlainTxt('sql-11'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">SQL:</span><div
id="sql-11"><div
class="sql"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #993333; font-weight: bold;">REPLACE</span> <span
style="color: #993333; font-weight: bold;">INTO</span> <span
style="color: #ff0000;">`transcripts`</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #993333; font-weight: bold;">SET</span> <span
style="color: #ff0000;">`ensembl_transcript_id`</span> = <span
style="color: #ff0000;">'ENSORGT00000000001'</span>,</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff0000;">`transcript_chrom_start`</span> = <span
style="color: #cc66cc;color:#800000;">12345</span>,</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff0000;">`transcript_chrom_end`</span> = <span
style="color: #cc66cc;color:#800000;">12678</span>;</div></li></ol></div></div></div><p></p><p>If the record exists, it will be overwritten; if it does not yet exist, it will be created.<br
/> However, using this method isn't efficient for our case: we do not need to overwrite existing records, it's fine just to skip them.</p><p>Method 2: using INSERT IGNORE<br
/> Also very simple:</p><div
class="igBar"><span
id="lsql-12"><a
href="#" onclick="javascript:showPlainTxt('sql-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">SQL:</span><div
id="sql-12"><div
class="sql"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #993333; font-weight: bold;">INSERT</span> <span
style="color: #993333; font-weight: bold;">IGNORE</span> <span
style="color: #993333; font-weight: bold;">INTO</span> <span
style="color: #ff0000;">`transcripts`</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #993333; font-weight: bold;">SET</span> <span
style="color: #ff0000;">`ensembl_transcript_id`</span> = <span
style="color: #ff0000;">'ENSORGT00000000001'</span>,</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff0000;">`transcript_chrom_start`</span> = <span
style="color: #cc66cc;color:#800000;">12345</span>,</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff0000;">`transcript_chrom_end`</span> = <span
style="color: #cc66cc;color:#800000;">12678</span>;</div></li></ol></div></div></div><p></p><p>Here, if the 'ensembl_transcript_id' is already present in the database, it will be silently skipped (ignored). (To be more precise, here's a quote from MySQL reference manual: "If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted.".) If the record doesn't yet exist, it will be created.</p><p>This second method has several potential weaknesses, including non-abortion of the query in case any other problem occurs (<a
href="http://dev.mysql.com/doc/refman/5.0/en/insert.html">see the manual</a>). Thus it should be used if previously tested without the IGNORE keyword.</p><p>There is one more option: to use <a
href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html">INSERT ... ON DUPLICATE KEY UPDATE syntax</a>, and in the UPDATE part just <del
datetime="2008-08-30T14:13:11+00:00">do nothing</del> do some meaningless (empty) operation, like calculating 0+0 (<a
href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-80275">Geoffray</a> <ins
datetime="2008-08-30T14:13:11+00:00">suggests doing the id=id assignment for the MySQL optimization engine to ignore this operation</ins>). Advantage of this method is that it only ignores duplicate key events, and still aborts on other errors.</p><p>As a final notice: this post was inspired by <a
href="http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/">Xaprb</a>. I'd also advise to consult his other <a
href="http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/">post on writing flexible SQL queries</a>.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&amp;title=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" id="wpa2a_12"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html/feed</wfw:commentRss> <slash:comments>42</slash:comments> </item> <item><title>Useful Python looping techniques</title><link>http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</link> <comments>http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html#comments</comments> <pubDate>Wed, 26 Sep 2007 09:21:00 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[example]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[looping]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</guid> <description><![CDATA[These are all excerpts from the Python documentation. To synchronously and simultaneously loop over two sequences: PLAIN TEXT PYTHON: questions = &#91;'name', 'quest', 'favourite colour'&#93; answers = &#91;'Lancelot', 'the holy grail', 'blue'&#93; &#160; for q, a in zip&#40;questions, answers&#41;: &#160; &#160; print 'What is your %s?&#160; It is %s.' % &#40;q, a&#41; To loop over [...]]]></description> <content:encoded><![CDATA[<p>These are all excerpts from the Python documentation.</p><p>To synchronously and simultaneously loop over two sequences:</p><div
class="igBar"><span
id="lpython-17"><a
href="#" onclick="javascript:showPlainTxt('python-17'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-17"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">questions = <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'name'</span>, <span
style="color: #483d8b;">'quest'</span>, <span
style="color: #483d8b;">'favourite colour'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">answers = <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'Lancelot'</span>, <span
style="color: #483d8b;">'the holy grail'</span>, <span
style="color: #483d8b;">'blue'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> q, a <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">zip</span><span
style="color: black;">&#40;</span>questions, answers<span
style="color: black;">&#41;</span>:</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">'What is your %s?&nbsp; It is %s.'</span> % <span
style="color: black;">&#40;</span>q, a<span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><p>To loop over a sequence with both key and value:<br
/> <span
id="more-229"></span></p><div
class="igBar"><span
id="lpython-18"><a
href="#" onclick="javascript:showPlainTxt('python-18'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-18"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> i, v <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">enumerate</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'tic'</span>, <span
style="color: #483d8b;">'tac'</span>, <span
style="color: #483d8b;">'toe'</span><span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> i, v</div></li></ol></div></div></div><p></p><p>To loop over dictionary, again with key and value:</p><div
class="igBar"><span
id="lpython-19"><a
href="#" onclick="javascript:showPlainTxt('python-19'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-19"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">knights = <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'gallahad'</span>: <span
style="color: #483d8b;">'the pure'</span>, <span
style="color: #483d8b;">'robin'</span>: <span
style="color: #483d8b;">'the brave'</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> k, v <span
style="color: #ff7700;font-weight:bold;">in</span> knights.<span
style="color: black;">iteritems</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k, v</div></li></ol></div></div></div><p></p><p>To loop over some sorted sequence, but without modifying the original sequence (beware the use of set() in this example):</p><div
class="igBar"><span
id="lpython-20"><a
href="#" onclick="javascript:showPlainTxt('python-20'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-20"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">basket = <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'apple'</span>, <span
style="color: #483d8b;">'orange'</span>, <span
style="color: #483d8b;">'apple'</span>, <span
style="color: #483d8b;">'pear'</span>, <span
style="color: #483d8b;">'orange'</span>, <span
style="color: #483d8b;">'banana'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> f <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">sorted</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">set</span><span
style="color: black;">&#40;</span>basket<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> f</div></li></ol></div></div></div><p></p><p>For looping sorted dictionaries, see also <a
href="http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html">How to sort Python dict (dictionary)</a>.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;title=Useful%20Python%20looping%20techniques" id="wpa2a_16"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>How to sort Python dict (dictionary)</title><link>http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html</link> <comments>http://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>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 PLAIN TEXT PYTHON: #!/usr/local/bin/python &#160; author = &#123;"php":"Rasmus Lerdorf",\ &#160; &#160; "perl":"Larry Wall",\ &#160; &#160; "tcl":"John Ousterhout",\ &#160; &#160; "awk":"Brian Kernighan",\ &#160; &#160; "java":"James Gosling",\ &#160; &#160; "parrot":"Simon Cozens",\ &#160; &#160; "python":"Guido van Rossum"&#125; &#160; langs = author.keys&#40;&#41; [...]]]></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
class="igBar"><span
id="lpython-23"><a
href="#" onclick="javascript:showPlainTxt('python-23'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-23"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;">#!/usr/local/bin/python</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">author = <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">"php"</span>:<span
style="color: #483d8b;">"Rasmus Lerdorf"</span>,\</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"perl"</span>:<span
style="color: #483d8b;">"Larry Wall"</span>,\</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"tcl"</span>:<span
style="color: #483d8b;">"John Ousterhout"</span>,\</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"awk"</span>:<span
style="color: #483d8b;">"Brian Kernighan"</span>,\</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"java"</span>:<span
style="color: #483d8b;">"James Gosling"</span>,\</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"parrot"</span>:<span
style="color: #483d8b;">"Simon Cozens"</span>,\</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"python"</span>:<span
style="color: #483d8b;">"Guido van Rossum"</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">langs = 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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">langs.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> language,<span
style="color: #483d8b;">"is the child of"</span>,author<span
style="color: black;">&#91;</span>language<span
style="color: black;">&#93;</span></div></li></ol></div></div></div><p></p><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
class="igBar"><span
id="lpython-24"><a
href="#" onclick="javascript:showPlainTxt('python-24'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-24"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">def</span> ksort<span
style="color: black;">&#40;</span>d, func = <span
style="color: #008000;">None</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; keys = d.<span
style="color: black;">keys</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> keys</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># example use, assume 'd' is a dictionary</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k, d<span
style="color: black;">&#91;</span>k<span
style="color: black;">&#93;</span></div></li></ol></div></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_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;title=How%20to%20sort%20Python%20dict%20%28dictionary%29" id="wpa2a_20"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
