0

I have a list, cntrs, which are co-ordinates for the number of centres in a cluster. Basically, the first element is an array of 2 co-ordinates, followed by an array with 3 co-ordinates and so forth. Its contents with print cntrs is:

[array([[ 1.95149888,  1.95403504],
       [ 6.98414983,  6.9892335 ]]), array([[ 0.98186521,  0.97763684],
       [ 7.01523632,  7.01965819],
       [ 3.02779515,  3.04964209]]), array([[ 0.97749825,  0.97886934],
       [ 7.01564811,  7.01963124],
       [ 2.90672846,  2.86009831],
       [ 3.16718753,  3.27143671]]), array([[ 3.16520274,  3.27132643],
       [ 6.72910999,  6.94502491],
       [ 0.97715149,  0.97907607],
       [ 2.90738599,  2.85892006],
       [ 7.29924758,  7.11088622]]), array([[ 7.29993897,  7.11152081],
       [ 3.16770976,  3.27348032],
       [ 6.72987889,  6.94502259],
       [ 1.32396815,  0.92670203],
       [ 0.74383915,  0.99317788],
       [ 2.90918296,  2.86325108]]), array([[ 3.04767311,  2.74427507],
       [ 0.74433306,  0.99277782],
       [ 2.75432472,  3.02288522],
       [ 1.32445471,  0.92522911],
       [ 7.30035779,  7.11196209],
       [ 6.73027771,  6.94494962],
       [ 3.19404203,  3.2858904 ]]), array([[ 3.16693842,  3.27337917],
       [ 0.71526946,  0.97641053],
       [ 1.19666632,  1.15407263],
       [ 1.41534598,  0.70413407],
       [ 7.3898936 ,  7.23016845],
       [ 6.62321659,  6.95167364],
       [ 7.06769041,  6.94257899],
       [ 2.90896473,  2.8637164 ]]), array([[ 7.30000058,  7.111688  ],
       [ 2.93568071,  2.76502603],
       [ 1.19635564,  1.15350525],
       [ 3.17086746,  3.28597849],
       [ 2.73214961,  3.05243834],
       [ 1.41510711,  0.70335533],
       [ 3.28473941,  2.82685228],
       [ 6.72963674,  6.94498021],
       [ 0.71540198,  0.97553514]]), array([[ 3.04671636,  2.74375001],
       [ 3.19246634,  3.28516669],
       [ 7.42117955,  7.25403886],
       [ 7.12337213,  6.72896363],
       [ 6.80853771,  6.82932201],
       [ 6.56014649,  6.96719779],
       [ 2.75383971,  3.02254777],
       [ 1.32533361,  0.92380979],
       [ 7.10911346,  7.07564848],
       [ 0.74497763,  0.99293499]])]

I would like to format that output to 2 decimal places. However, I'm not sure how as the typical print "{:.2f}".format(cntrs[i]) does not work. This is what I have so far:

for i in range(len(cntrs)):
    print "Number of Centres: ", i+2
    print "Co-ordinates: ", cntrs[i]

And it outputs, for example:

Number of Centres:  2
Co-ordinates:  [[ 1.95149888  1.95403504]
 [ 6.98414983  6.9892335 ]]

Number of Centres:  3
Co-ordinates:  [[ 0.98186521  0.97763684]
 [ 7.01523632  7.01965819]
 [ 3.02779515  3.04964209]]

How can I format the coordinates better, to 2 dp?

Undead
  • 125
  • 2
  • 7
  • You can use `np.around(cntrs[i], decimals=2)` – yuji Feb 23 '18 at 15:57
  • Thank you, this worked! I noticed whole values were simply displayed as "3.". Would there be any way to pad the zeros for "3.00" for consitency? – Undead Feb 23 '18 at 16:04
  • Have a look at [How to pretty-printing a numpy.array without scientific notation and with given precision?](https://stackoverflow.com/questions/2891790/how-to-pretty-printing-a-numpy-array-without-scientific-notation-and-with-given) – Mr. T Feb 23 '18 at 16:28

1 Answers1

0

Matrixes always get messy but you basically have a list of lists and you need to format each integer inside the lists. The shortest way to do that, in my opinion, is using a nested list comprehension. Beware that the performance is not great at O(n^2):

for i in range(len(cntrs)):
  print("Number of Centres: ", i + 2)
  print("Co-ordinates: ", [["%.2f" % centre for centre in centre_arr] 
  for centre_arr in cntrs[i]])

Hope it helps.

randyjp
  • 1,084
  • 9
  • 8