简体   繁体   中英

How do I use the pretty print module to print a list in python?

I've been searching this but I can't find it. I want to view a list of commands in an organized fashion, so that this:

Comandos = ["1: Imprime lista de opciones", "2: Abre la calculadora de dinero"]

pprint(Comandos)

Gets printed like this:

1: Imprime lista de opciones
2: Abre la calculadora de dinero.

您不需要pprint ,只需join它们连接在一起并print即可:

print('\n'.join(Comandos))

As @willem-van-onsem said, you can simply join and it will be all done.

However if you need HAVE TO to use pprint you can do this as such:

pprint(a,width=max(len(a[0]),len(a[1]))*2)

Mind you, it is not optimal (nor recommended) solution.

And more general approach :

pprint(a,width=len((max(a, key=len))*2))

Additional note: This appropach will not work when n (n>1) of shortest strings sum to the longest (on length).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM