0

How can I style text like numpy or pandas does?

I tried using pprint, but it either was not what I was expecting.

Let's say I have a list [[0 for w in range(1000)] for h in range(1000)]. I would like to print it similar to this:

From pandas:

                   A         B         C         D
2000-01-01  0.469112 -0.282863 -1.509059 -1.135632
2000-01-02  1.212112 -0.173215  0.119209 -1.044236
2000-01-03 -0.861849 -2.104569 -0.494929  1.071804
2000-01-04  0.721555 -0.706771 -1.039575  0.271860
2000-01-05 -0.424972  0.567020  0.276232 -1.087401
2000-01-06 -0.673690  0.113648 -1.478427  0.524988
2000-01-07  0.404705  0.577046 -1.715002 -1.039268
2000-01-08 -0.370647 -1.157892 -1.344312  0.844885

From numpy

 [[ 1.1  0.9  0. ]
  [ 1.1  0.9  0. ]
  [ 1.1  0.9  0. ]]

Especially if you have a big table both pandas and numpy display table only partially

  [[ 0.,  0.,  0., ...,  0.,  0.,  0.],
   [ 0.,  0.,  0., ...,  0.,  0.,  0.],
   [ 0.,  0.,  0., ...,  0.,  0.,  0.],
   ..., 
   [ 0.,  0.,  0., ...,  0.,  0.,  0.],
   [ 0.,  0.,  0., ...,  0.,  0.,  0.],
   [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

How can I achieve this? Is there a library for it? can pprint do it?

Zloutek1
  • 55
  • 6
  • 1
    `pprint` is fairly limited in what it can do, and it's not very configurable. It's designed to make (slightly) nicer output for the developer, it's not intended to be used to produce output that the user will see. – PM 2Ring Oct 05 '17 at 15:34
  • Your question is unclear. What is your actual input? – Mad Physicist Oct 05 '17 at 15:37
  • Seems like a duplicate: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data – ayhan Oct 05 '17 at 15:38
  • so the input is a regular python 2d list such as [[0, 1, 2], [3, 4, 5], [6, 7, 'eight'], ['nine', 10]] – Zloutek1 Oct 05 '17 at 15:43
  • @ayhan but it does not really answer how to shorten the output if it is "overflowing", meaning that it would add the dots in between such as [ 0., ..., 0.] – Zloutek1 Oct 05 '17 at 15:44

1 Answers1

0

It seems you actually don't need a library for that.

Suppose your list is :

l = [[0 for w in range(10)] for h in range(10)]

Effectively like you say output is :

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

To display it like an array you merely need to convert the list into a numpy array as does the following script :

import numpy as np

l = [[0 for w in range(10)] for h in range(10)]
l = np.asarray(l)

print(l)

Output :

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Laurent B.
  • 1,653
  • 1
  • 7
  • 16