0

I have a huge list with hundreds of thousands of numbers. The list is badly formatted when produced by sagemath, but i must have it in the kind of formatting shown below to "feed" it to another function. Here is sample of the well formatted list:

           C=[
           (  7.850629, 25.421135, 22.162571),
           ( 37.706629, 28.421472,  0.229876),
           ( 37.560629, 21.421809, 18.320977),
           ( 39.238629, 26.422147, 18.442572),
           ( 35.087169,  0.419785, 15.055789),
           ]

As you can see, all elements are aligned to the right as well as based on the precision and decimal place. So my question is this: How can i convert the badly formatted list (example shown below) to the well formatted above

    B=[(37.074945, 22.414327,
9.756234), (37.074945, 22.414665,
1.669214), (37.074945, 22.415002,
8.571376), (37.074945, 22.41534,
1.294731), (37.074945, 22.415677,
5.753062), (37.074945, 22.416014,
7.519850)]

(If i could describe it even a bit more, i would say that if my list is thought of as a matrix, then it would have tenths of thousands of rows and three columns)

CosmoSurreal
  • 259
  • 6
  • 16

2 Answers2

0

Sage doesn't have good alignment functions built-in. If you're willing to think of your list as a matrix, you can do

sage: matrix(B)
[37.0749450000000 22.4143270000000 9.75623400000000]
[37.0749450000000 22.4146650000000 1.66921400000000]
[37.0749450000000 22.4150020000000 8.57137600000000]
[37.0749450000000 22.4153400000000 1.29473100000000]
[37.0749450000000 22.4156770000000 5.75306200000000]
[37.0749450000000 22.4160140000000 7.51985000000000]

It's missing the commas and other syntactical pieces, but it's aligned properly. If you're willing to work with html output, you can do html.table(B). In the future (see this possible future enhancement) there ought to be a good "table" function for nice displays like this in general.

John Palmieri
  • 1,531
  • 8
  • 13
  • The problem is that the example data was not good ,well, for an example. Matrix won't align the elements according to decimal place C=[(37.074945, 22.414327, 9.756234), (37.074945, 22.414665, 1.669214), (37.074945, 22.415002, 8.571376), (37.074945, 22.41534, 18.294731), (37.074945, 22.415677, 5.753062), (37.074945, 22.416014, 17.519850)] you see this by trying out with this one. Nevertheless i accomplished my current task because of your help.Thank you very much – CosmoSurreal Jul 10 '12 at 13:59
0

Well, I think that's a general Python question. Given the C in your comment, the following work. The "%9.5f" is the key insight:

for l in C:
    print "(%s)," % ', '.join("%9.5f"%i for i in l)
   ....:     
( 37.07494,  22.41433,   9.75623),
( 37.07494,  22.41466,   1.66921),
( 37.07494,  22.41500,   8.57138),
( 37.07494,  22.41534,  18.29473),
( 37.07494,  22.41568,   5.75306),
( 37.07494,  22.41601,  17.51985),
Harald Schilly
  • 1,098
  • 1
  • 14
  • 15