# enter the data like this
X={'A':(0,0),'B':(0,1),'C':(1,2),'D':(0,1)}
# size of grid
xi=map(tuple.__getitem__,X.values(),[1]*len(X))
yi=map(tuple.__getitem__,X.values(),[0]*len(X))
xrng = (min(xi), max(xi)+1)
yrng = (min(yi), max(yi)+1)
for y in range(*yrng): # rows
print '+' * ((xrng[1]-xrng[0])*3) + '+'
k={} # each item k[x] is list of elements in xth box in this row
for x in range(*xrng):
# list of items in this cell
k[x]=[u for u in X.keys() if X[u]==(y,x)]
h=max(map(len, k.values())) # row height
for v in range(h): # lines of row
c=[]
for x in range(*xrng): # columns
if k[x]:
c.append(k[x][0])
del k[x][0]
else: c.append(' ') # shorter cell
s="+ " + "+ ".join(c) + "+"
print s
print "+" * ((xrng[1]-xrng[0])*3) + '+'
感謝您的代碼! – yassin 2010-09-27 16:29:36