Best way to generate alternate toggling values in a loop?
Brian Blais
bblais at bryant.edu
Thu Oct 18 08:42:26 EDT 2007
On Oct 18, 2007, at Oct 18:7:47 AM, Paul Hankin wrote:
> On Oct 18, 12:11 pm, "Amit Khemka" <khemkaa... at gmail.com> wrote:
>> Why not just use enumerate ?
>>
>> clvalues = ["Even", "Odd"]
>> for i, (id, name) in enumerate(result):
>> stringBuffer.write('''
>> <tr class="%s">
>> <td>%d</td>
>> <td>%s</td>
>> </tr>
>> '''
>> %
>> (clvalues[i % 2], id, name))
>
> I like this code: straightforward and pragmatic.
Although the itertools is probably the way to go here, I've used the
following construct, especially in two-player games. I like the
readability of it:
value='Even'
other_value='Odd'
for i, (id,name) in enumerate(result):
stringBuffer.write('''
<tr class="%s">
<td>%d</td>
<td>%s</td>
</tr>
''' % (value,id,name)
value,other_value=other_value,value # swap the value
bb
--
Brian Blais
bblais at bryant.edu
http://web.bryant.edu/~bblais
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071018/b72a11f7/attachment.html>
More information about the Python-list
mailing list