| | 80 | |
| | 81 | === Example usage === |
| | 82 | |
| | 83 | {{{ |
| | 84 | #!xml |
| | 85 | <table py:with="fields = ['a', 'b', 'c', 'd', 'e']"> |
| | 86 | <tr py:for="row in group(fields, 2)"> |
| | 87 | <td py:for="cell in row">${cell}</td> |
| | 88 | </tr> |
| | 89 | </table> |
| | 90 | }}} |
| | 91 | |
| | 92 | That should result in the following output: |
| | 93 | |
| | 94 | {{{ |
| | 95 | #!xml |
| | 96 | <table> |
| | 97 | <tr> |
| | 98 | <td>a</td><td>b</td> |
| | 99 | </tr><tr> |
| | 100 | <td>c</td><td>d</td> |
| | 101 | </tr><tr> |
| | 102 | <td>e</td><td></td> |
| | 103 | </tr> |
| | 104 | </table> |
| | 105 | }}} |
| | 106 | |
| | 107 | == countoccurrences() == |
| | 108 | |
| | 109 | Written by Arnar Birgisson and shared on the IrcChannel. |
| | 110 | |
| | 111 | {{{ |
| | 112 | #!python |
| | 113 | def countoccurrences(numbers, minlength=0): |
| | 114 | """Takes a list of integers numbers, in the range of 0..n and returns |
| | 115 | a list of integers where i-th item is the number of times i appears in types. |
| | 116 | |
| | 117 | If minlength is specified and n+1 < minlength, the returned list is |
| | 118 | right-padded with zeroes to make it contain minlength items. |
| | 119 | |
| | 120 | Examples: |
| | 121 | >>> countoccurrences([0,3,1,2,1,1,3,5]) |
| | 122 | [1,3,1,2,0,1] |
| | 123 | >>> countoccurrences([0,3,1,2,1,1,3,5], 10) |
| | 124 | [1,3,1,2,0,1,0,0,0,0] |
| | 125 | """ |
| | 126 | # TODO come up with a better name |
| | 127 | counts = [0] * max(max(numbers)+1, minlength) |
| | 128 | for x in numbers: |
| | 129 | counts[x] += 1 |
| | 130 | return counts |
| | 131 | }}} |
| | 132 | |
| | 133 | Example usage: |
| | 134 | |
| | 135 | {{{ |
| | 136 | #!xml |
| | 137 | <py:with vars="counts = countoccurrences([p.status.value for p in subdir.job.pages], 3)"> |
| | 138 | <td><span style="color: #e18f01;">${counts[0]}</span></td> |
| | 139 | <td><span style="color: #249f0b;">${counts[1]}</span></td> |
| | 140 | <td><span style="color: #ae0a0a;">${counts[2]}</span></td> |
| | 141 | </py:with> |
| | 142 | }}} |
| | 143 | |
| | 144 | ---- |
| | 145 | See also: MarkupGuide, MarkupRecipes |