[Tutor] Printing a formatted list (of lists)
Tue, 30 Jul 2002 02:19:15 -0700
On 29/7/02, Jeff Shannon wrote:
>> for j in range(len(table)):
>> print `table[j]`.rjust(columnwidth),
>Well, the above snippet can be simplified as
>for cell in table:
> print cell.rjust(columnwidth),
That is prettier. Though I didn't quite tell everything--in several
places I have basically the same thing, but inside of another loop,
with things to print on the line before and after the list.
for i in range(len(name)):
for j in range(len(values[i])):
>If you know the number (and width) of columns in your table, then you can do
>something like this:
>for line in table:
> print "%15s %15s %15s %15s" % tuple(line)
Tuples! I knew there must be a use for tuples! :-) I kept trying to
do something similar with lists, and it didn't like that.
>If you *don't* know the number of columns
>If you don't know what your maximum column width should be
I expect that this sort of thing comes up often enough that it's worth
writing up something general that can figure out how many of this and
that, and all the widths and such itself. Think once, day dream ad
>Hope this at least gives you some ideas...
Yes indeed. Especially the tuple keys to the kingdom! Many thanks.
Allyn Weaks firstname.lastname@example.org Seattle, WA Sunset zone 5
Pacific NW Native Wildlife Gardening: http://www.tardigrade.org/natives/
"The benefit of even limited monopolies is too doubtful, to be opposed
to that of their general suppression." Thomas Jefferson