Any way to properly pretty-print ordered dictionaries?

后端 未结 15 1518
春和景丽
春和景丽 2020-12-07 14:34

I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window

15条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 15:01

    If the dictionary items are all of one type, you could use the amazing data-handling library pandas:

    >>> import pandas as pd
    >>> x = {'foo':1, 'bar':2}
    >>> pd.Series(x)
    bar    2
    foo    1
    dtype: int64
    

    or

    >>> import pandas as pd
    >>> x = {'foo':'bar', 'baz':'bam'}
    >>> pd.Series(x)
    baz    bam
    foo    bar
    dtype: object
    

提交回复
热议问题