[Tutor] A (not so) simple question? Round Robin
Christopher Smith
csmith@blakeschool.org
Wed, 20 Jun 2001 12:35:11 -0500
Thanks to Roland and Greg for comments regarding the Round Robin Code.
Since then I have been reading the Language Reference and learned about
the "list comprehension" approach to handling lists. Here is the way
I now can extract certain keys and then extract the corresponding
entries. I already have a dictionary named "t" with tuple keys which
are pairs of integers (i,j). I want to print out all entries having a
key with a given value of i:
k=t.keys()
k.sort() # so they are in column order when the row is extracted
for i in range(n):
# get the keys for this row and corresponding entries
row=[x for x in k if x[0]==i]
p=[t[x] for x in row]
# print them
print "%s |" % teams[i] + "%3d"*n % tuple(p)
I noted also in reading (and in Greg's code) that lists of lists are
the preferred way to do matrices. Introducing this eliminates the
need for the above "dictionary row extraction."
Here's an alternate way to initialize an array (i.e. list of lists)
# function to initialize array
def make_array(r,c):
a=[]
for i in range(r):
a.append([])
for ii in range(c):
a[i].append(0)
return a
/c
-------------------------------------------------------------
Have you ever sung in Latin? The sounds are so nice to sing.
Did you ever code in Python? It's very much the same thing.