How can I write sequential keys of a dictionary?

18 views
Skip to first unread message

satoshii

unread,
Apr 19, 2012, 1:58:50 AM4/19/12
to we...@googlegroups.com
Hello.
Please see the code below:
 
    <table>
      $for r in xrange(31):
      <tr>
        $for c in xrange(31):
        <td>
          $if r == 0 and c == 0: $pass
          $elif r == 0: $goesc[c-1].name
          $elif c == 0: $goesr[r-1].gou
          $else:
            $if r == c:
              <input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c1']" />
            $else:
              <input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text" placeholder='$r->$c' value="$nicks['r1c1']" />
        </td>
      </tr>
    </table>

Look at the variable named "nicks".
It's a dictionary those keys are from "r1c1" to "r30c30".
I want to write like this, but it shows error.

$nicks['r$(r)c$(c)'] 

How can I write sequential keys of a dictionary or do I have to write like this?

        <td><input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c1']" /></td> 
        <td><input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c2']" /></td> 
        <td><input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c3']" /></td> 
        <td><input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c4']" /></td> 
        <td><input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c5']" /></td> 

Anand Chitipothu

unread,
Apr 19, 2012, 2:16:58 AM4/19/12
to we...@googlegroups.com
On Thu, Apr 19, 2012 at 11:28 AM, satoshii <satoshi...@gmail.com> wrote:
Hello.
Please see the code below:
 
    <table>
      $for r in xrange(31):
      <tr>
        $for c in xrange(31):
        <td>
          $if r == 0 and c == 0: $pass
          $elif r == 0: $goesc[c-1].name
          $elif c == 0: $goesr[r-1].gou
          $else:
            $if r == c:
              <input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text same" placeholder='$r->$c' value="$nicks['r1c1']" />
            $else:
              <input type="text" id="r$(r)c$(c)" name="r$(r)c$(c)" class="text" placeholder='$r->$c' value="$nicks['r1c1']" />
        </td>
      </tr>
    </table>

Look at the variable named "nicks".
It's a dictionary those keys are from "r1c1" to "r30c30".
I want to write like this, but it shows error.

$nicks['r$(r)c$(c)'] 

You should use:

$nicks['r%dc%d' % (r, c)]

Python expression starts with a $. And after that it is Python, $ substitutions won't work and you have to use the Python rules there.

Anand

satoshii

unread,
Apr 19, 2012, 2:25:55 AM4/19/12
to we...@googlegroups.com
Thank you so much. I did it!
Reply all
Reply to author
Forward
0 new messages