Typical printing for python objects

When analyzing data in Ipython, I often have to look at the data simply by printing its contents into the shell. Numpy has the ability to show only fields of huge objects when they are too long. I really like this ndarrays function, but when I print an internal python object (like a dictionary with 15k objects in it), they are dumped onto the screen or sometimes truncated not very friendly. So, for example, for a huge dictionary that I would like to see in the output of something like this

{ '39416' : '1397', '39414' : '1397', '7629' : '7227', ..., '31058' : '9606', '21097' : '4062', '32040' : '9606' } 

It would be nice if you could take care of the alignment and nested data structures. Is their special module that can provide such functionality for the python base classes (list, dict)? Or are there some ipython configuration tricks that I don't know anything about?

+5
source share
3 answers

There is a good built-in pprint library. Take a look at this.

 >>> from pprint import pprint >>> pprint({x: list(range(x)) for x in range(10)}) {0: [], 1: [0], 2: [0, 1], 3: [0, 1, 2], 4: [0, 1, 2, 3], 5: [0, 1, 2, 3, 4], 6: [0, 1, 2, 3, 4, 5], 7: [0, 1, 2, 3, 4, 5, 6], 8: [0, 1, 2, 3, 4, 5, 6, 7], 9: [0, 1, 2, 3, 4, 5, 6, 7, 8]} 
+1
source

If your dictionary is well structured, you can convert it to a Pandas framework for viewing.

 import numpy as np import pandas as pd >>> pd.DataFrame({'random normal': np.random.randn(1000), 'random int': np.random.randint(0, 10, 1000)}) random int random normal 0 6 0.850827 1 7 0.486551 2 4 -0.111008 3 9 -1.319320 4 6 -0.393774 5 1 -0.878507 .. ... ... 995 2 -1.882813 996 3 -0.121003 997 3 0.155835 998 5 0.920318 999 2 0.216229 [1000 rows x 2 columns] 
+1
source

Formatting numpy has an ellipsis function; By default, it uses more than 1000 items.

pprint can make the display more enjoyable, but I don’t think it has an ellipsis function. But you can study his documents.

With a list I can use slice

 list(range(100))[:10] 

to see a limited number of values.

This is harder to do with the dictionary. With some trial and error, this works with restraint:

 {k:dd[k] for k in list(dd.keys())[:10]} 

(I'm on Py3, so you need an extra list ).

It's easy to write your own utility functions if you can't find something in pprint . It is also possible that some packages on pypi do this. For example, a quick search turned out to be

https://pypi.python.org/pypi/pprintpp

pprintpp , which claims to be really good. But, like the pprint stock, he is more interested in the depth of nesting of lists and dictionaries, rather than their length.

0
source

All Articles