I am not really sure of a better way but if your looking for a way to make your code cleaner or more efficient <div>you can try Numpy - <a href="http://numpy.scipy.org/">http://numpy.scipy.org/</a><div>
<br><br><div class="gmail_quote">On Tue, Mar 2, 2010 at 4:54 PM, David Eccles (gringer) <span dir="ltr">&lt;<a href="mailto:gmail@gringer.org">gmail@gringer.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

I&#39;ve managed to drum up some code to obtain a list containing joined diagonal<br>
elements of a matrix (I&#39;m making a word finder generator), but am wondering if<br>
there&#39;s any better way to do this:<br>
<br>
# setup so code snippet works properly<br>
sizeW = 4<br>
sizeH = 3<br>
puzzleLayout = [&#39;spam&#39;] * (sizeW * sizeH)<br>
<br>
# Starting with, say, an array with these indices:<br>
# 0 1 2 3<br>
# 4 5 6 7<br>
# 8 9 A B<br>
<br>
# I want the following items for a back diagonal (not in square brackets):<br>
# [-2],[3],8 (+5) | div 4 = -1, 1, 2<br>
# [-1],4,9 (+5)   | div 4 = -1, 1, 2<br>
# 0,5,A (+5)      | div 4 =  0, 1, 2<br>
# 1,6,B (+5)      | div 4 =  0, 1, 2<br>
# 2,7,[C] (+5)    | div 4 =  0, 1, 3<br>
# 3,[8],[D] (+5)  | div 4 =  0, 2, 3<br>
<br>
# in other words, increase sequence by sizeW + 1 each time (sizeW - 1<br>
# for forward diagonals), only selecting if the line you&#39;re on matches<br>
# the line you want to be on<br>
<br>
# back as in backslash-like diagonal<br>
puzzleDiagBack = [(&#39;&#39;.join([puzzleLayout[pos*(sizeW+1) + i] \<br>
for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos])) \<br>
for i in range(-sizeH+1,sizeW)]<br>
puzzleDiagBackRev = [(&#39;&#39;.join(reversed([puzzleLayout[pos*(sizeW+1) + i] \<br>
for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos]))) \<br>
for i in range(-sizeH+1,sizeW)]<br>
# fwd as in forwardslash-like diagonal<br>
puzzleDiagFwdRev = [(&#39;&#39;.join([puzzleLayout[pos*(sizeW-1) + i] \<br>
for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos])) \<br>
for i in range(sizeW+sizeH-1)]<br>
puzzleDiagFwd = [(&#39;&#39;.join(reversed([puzzleLayout[pos*(sizeW-1) + i] \<br>
for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos]))) \<br>
for i in range(sizeW+sizeH-1)]<br>
<br>
Cheers,<br>
<font color="#888888">David Eccles (gringer)<br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</font></blockquote></div><br></div></div>