[Tutor] formatting and pretty printing nested structures
Gregor Lingl
glingl at aon.at
Mon Feb 23 04:01:20 EST 2004
kevin parks schrieb:
> Hi. I have a list of lists. Inside the list are lists that are made up
> of strings and floats. When i print the list it prints the floats in
> the usual insane long wierdo format and i want to print them with
> string formatting so that they are more readable, so i somehow have to
> pick apart the nested items so that i can print:
>
> 0 ['C', 8.0, 8.0]
> 1 ['C#', 8.0099999999999998, 8.0833349227905273]
>
> more like this:
>
> 0 ['C', 8.00, 8.000000]
> 1 ['C#', 8.01, 8.083333]
>
> or even betterererer:
>
> 0 C 8.00 8.000000
> 1 C# 8.01 8.083333
>
> What is the best way to format and print nested srtucres like i have
> here?
>
Hi Kevin!
Not having a module named rtcmix at hand nor knowing the
function octpch and even not knowing if the following is
the *best way*, I recommend
>>> mylist = [['C', 8.0, 8.0], ['C#', 8.0099999999999998,
8.0833349227905273]]
>>> for i, item in enumerate(mylist):
print "%2d %-2s %4.2f%10.6f" % tuple([i]+item)
0 C 8.00 8.000000
1 C# 8.01 8.083335
or, if enumerate doesn't exist in your Python version:
>>> for i in range(len(mylist)):
print "%2d %-2s %4.2f%10.6f" % tuple([i]+mylist[i])
0 C 8.00 8.000000
1 C# 8.01 8.083335
Regards,
Gregor
> cheers,
>
> kevin
>
> -- -----
>
> from rtcmix import *
>
> c = ['C', 8.00, octpch(8.00)]
>
More information about the Tutor
mailing list