/usr/share/doc/pyxplot/html/sect0044.html is in pyxplot-doc 0.9.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="generator" content="plasTeX" />
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<title>PyXPlot Users' Guide: Sorting lists</title>
<link href="sect0045.html" title="Iterating over lists" rel="next" />
<link href="sect0043.html" title="Using lists as buffers" rel="prev" />
<link href="sect0041.html" title="Lists" rel="up" />
<link rel="stylesheet" href="styles/styles.css" />
</head>
<body>
<div class="navigation">
<table cellspacing="2" cellpadding="0" width="100%">
<tr>
<td><a href="sect0043.html" title="Using lists as buffers"><img alt="Previous: Using lists as buffers" border="0" src="icons/previous.gif" width="32" height="32" /></a></td>
<td><a href="sect0041.html" title="Lists"><img alt="Up: Lists" border="0" src="icons/up.gif" width="32" height="32" /></a></td>
<td><a href="sect0045.html" title="Iterating over lists"><img alt="Next: Iterating over lists" border="0" src="icons/next.gif" width="32" height="32" /></a></td>
<td class="navtitle" align="center">PyXPlot Users' Guide</td>
<td><a href="index.html" title="Table of Contents"><img border="0" alt="" src="icons/contents.gif" width="32" height="32" /></a></td>
<td><a href="sect0288.html" title="Index"><img border="0" alt="" src="icons/index.gif" width="32" height="32" /></a></td>
<td><img border="0" alt="" src="icons/blank.gif" width="32" height="32" /></td>
</tr>
</table>
</div>
<div class="breadcrumbs">
<span>
<span>
<a href="index.html">PyXPlot Users' Guide</a> <b>:</b>
</span>
</span><span>
<span>
<a href="sect0001.html">Introduction to PyXPlot</a> <b>:</b>
</span>
</span><span>
<span>
<a href="chap-progDataTypes.html">Programming: Pyxplot’s data types</a> <b>:</b>
</span>
</span><span>
<span>
<a href="sect0041.html">Lists</a> <b>:</b>
</span>
</span><span>
<span>
<b class="current">Sorting lists</b>
</span>
</span>
<hr />
</div>
<div><h2 id="a0000000045">6.3.3 Sorting lists</h2>
<p>Methods are provided for sorting data in lists. The simplest of these is the <tt class="tt">sort()</tt> method, which sorts the members into order of ascending value.<a href="#a0000000613" class="footnote"><sup class="footnotemark">1</sup></a> The <tt class="tt">reverse()</tt> method can be used to invert the order of the list afterwards if descending order is wanted. </p><p> <tt class="ttfamily">pyxplot> <b class="bfseries">a = [8,4,7,3,6,2]</b></tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print a.sort()</b></tt><br /><tt class="ttfamily">[2, 3, 4, 6, 7, 8]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print a.reverse()</b></tt><br /><tt class="ttfamily">[8, 7, 6, 4, 3, 2]</tt> </p><h3 id="a0000000614">Custom sorting</h3>
<p>Often, however, a custom ordering is wanted. The <tt class="tt">sortOn(f)</tt> method takes a function of two arguments as its input. The function <tt class="tt">f(a,b)</tt> should return <img src="images/img-0156.png" alt="$-1$" style="vertical-align:0px;
width:21px;
height:12px" class="math gen" /> if <tt class="tt">a</tt> is to be placed before <tt class="tt">b</tt> in the sorted list, <img src="images/img-0131.png" alt="$1$" style="vertical-align:0px;
width:7px;
height:12px" class="math gen" /> if <tt class="tt">a</tt> is to be placed after <tt class="tt">b</tt> in the sorted list, and zero if the two elements have equal ranking. </p><p>The <tt class="tt">cmp(a,b)</tt> function is often useful in making comparison functions for use with the <tt class="tt">sortOn(f)</tt> method: it returns either <img src="images/img-0156.png" alt="$-1$" style="vertical-align:0px;
width:21px;
height:12px" class="math gen" />, <img src="images/img-0155.png" alt="$0$" style="vertical-align:0px;
width:9px;
height:12px" class="math gen" /> or <img src="images/img-0131.png" alt="$1$" style="vertical-align:0px;
width:7px;
height:12px" class="math gen" /> depending on Pyxplot’s default way of comparing two objects. In the example below, we pass it the magnitude of <tt class="tt">a</tt> and <tt class="tt">b</tt> to sort a list in order of magnitude. </p><p> <tt class="ttfamily">pyxplot> <b class="bfseries">absCmp(a,b) = cmp(abs(a),abs(b))</b></tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">a = range(-8,8)</b></tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print a</b></tt><br /><tt class="ttfamily">vector(-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7)</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">a = a.list()</b></tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print a.sortOn(absCmp)</b></tt><br /><tt class="ttfamily">[0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print a.sort()</b></tt><br /><tt class="ttfamily">[-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]</tt> </p><p>In this example, the <tt class="tt">range(start,end,step)</tt> function is used to generate a raster of values between <img src="images/img-0293.png" alt="$-8$" style="vertical-align:0px;
width:22px;
height:12px" class="math gen" /> and <img src="images/img-0294.png" alt="$8$" style="vertical-align:0px;
width:9px;
height:12px" class="math gen" />. It outputs a vector, which is converted into a list using the vector’s <tt class="tt">list()</tt> method. More information about vectors is in Section <a href="sec-vectors.html">6.5</a>. </p><p>The subroutine command, which is often used to implement more complicated sorting functions, will be covered in Section <a></a>. For example, the function used above could have been written: </p><pre>
subroutine absCmp(a,b)
{
return cmp(abs(a),abs(b))
}
</pre><h3 id="a0000000615">Sorting lists of lists</h3>
<p>The <tt class="tt">sortOnElement(n)</tt> method can be used to sort a list of lists on the <img src="images/img-0025.png" alt="$n$" style="vertical-align:0px;
width:11px;
height:8px" class="math gen" />th sub-element of each sublist. </p><p> <tt class="ttfamily">pyxplot> <b class="bfseries">b = []</b></tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">b.append([1797,1828,"Schubert"])</b></tt><br /><tt class="ttfamily">[[1797, 1828, "Schubert"]]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">b.append([1770,1827,"Beethoven"])</b></tt><br /><tt class="ttfamily">[[1797, 1828, "Schubert"], [1770, 1827, "Beethoven"]]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">b.append([1756,1791,"Mozart"])</b></tt><br /><tt class="ttfamily">[[1797, 1828, "Schubert"], [1770, 1827, "Beethoven"], [1756, 1791, "Mozart"]]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print b.sortOnElement(0) </b></tt><i class="it"># Order of birth</i><br /><tt class="ttfamily">[[1756, 1791, "Mozart"], [1770, 1827, "Beethoven"], [1797, 1828, "Schubert"]]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print b.sortOnElement(1) </b></tt><i class="it"># Order of death</i><br /><tt class="ttfamily">[[1756, 1791, "Mozart"], [1770, 1827, "Beethoven"], [1797, 1828, "Schubert"]]</tt><br /><tt class="ttfamily">pyxplot> <b class="bfseries">print b.sortOnElement(2) </b></tt><i class="it"># Alphabetical order</i><br /><tt class="ttfamily">[[1770, 1827, "Beethoven"], [1756, 1791, "Mozart"], [1797, 1828, "Schubert"]]</tt> </p></div>
<div id="footnotes">
<p><b>Footnotes</b></p>
<ol>
<li id="a0000000613">Non-numeric items are assigned arbitrary but consistent values for the purposes of sorting. Booleans are always lower-valued than numbers, but numbers are lower-valued than lists. Longer lists are always higher valued than shorter lists; larger dictionaries are always higher-valued than smaller dictionaries.</li>
</ol>
</div>
<div class="navigation">
<table cellspacing="2" cellpadding="0" width="100%">
<tr>
<td><a href="sect0043.html" title="Using lists as buffers"><img alt="Previous: Using lists as buffers" border="0" src="icons/previous.gif" width="32" height="32" /></a></td>
<td><a href="sect0041.html" title="Lists"><img alt="Up: Lists" border="0" src="icons/up.gif" width="32" height="32" /></a></td>
<td><a href="sect0045.html" title="Iterating over lists"><img alt="Next: Iterating over lists" border="0" src="icons/next.gif" width="32" height="32" /></a></td>
<td class="navtitle" align="center">PyXPlot Users' Guide</td>
<td><a href="index.html" title="Table of Contents"><img border="0" alt="" src="icons/contents.gif" width="32" height="32" /></a></td>
<td><a href="sect0288.html" title="Index"><img border="0" alt="" src="icons/index.gif" width="32" height="32" /></a></td>
<td><img border="0" alt="" src="icons/blank.gif" width="32" height="32" /></td>
</tr>
</table>
</div>
<script language="javascript" src="icons/imgadjust.js" type="text/javascript"></script>
</body>
</html>
|