Using .format() to format a list with field width arguments

核能气质少年 提交于 2019-11-27 04:22:21

问题


I recently (finally?) started to use .format() and have a perhaps a bit obscure question about it.

Given

res = ['Irene Adler', 35,  24.798]

and

(1) print('{0[0]:10s} {0[1]:5d} {0[2]:.2f}'.format(res))
(2) print('{:{}s} {:{}d} {:{}f}'.format(res[0], 10, res[1], 5, res[2], .2))

work great and both print:

Irene Adler    35 24.80
Irene Adler    35 24.80

I didn't know that I could deal with lists as in (1) which is neat. I had seen about field width arguments (2) with the old % formatting before.

My question is about wanting to do something like this which combines (1) and (2):

(3) print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, 10, 5, .2))

However, I am unable to do this, and I haven't been able to figure out from the documentation if this even possible. It would be nice to just supply the list to be printed, and the arguments for width.

By the way, I also tried this (w/o luck):

args = (10, 5, .2)
(4) print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, args))

In both instances I got:

D:\Users\blabla\Desktop>python tmp.py
Traceback (most recent call last):
  File "tmp.py", line 27, in <module>
    print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, 10, 5, .2))
ValueError: cannot switch from manual field specification to automatic field numbering

D:\Users\blabla\Desktop>python tmp.py
Traceback (most recent call last):
  File "tmp.py", line 35, in <module>
    print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, args))
ValueError: cannot switch from manual field specification to automatic field numbering

I also tried using zip() to combine the two sequences without luck.

My question is:

Can I specify a list to be printed effectively doing what I was trying to unsuccessfully do in (3) and (4) (clearly if this is possible, I'm not using the right syntax) and if so, how?


回答1:


The error message

ValueError: cannot switch from manual field specification to automatic field numbering

pretty much says it all: You need to give explicit field indices everwhere, and

print('{0[0]:{1}s} {0[1]:{2}d} {0[2]:{3}f}'.format(res, 10, 5, .2))

works fine.




回答2:


If you want to use .format(res, args), you can specify all indices in the format string like that:

>>> print('{0[0]:{1[0]}s} {0[1]:{1[1]}d} {0[2]:{1[2]}f}'.format(res, args))
Irene Adler    35 24.80

But, if you want to make the format string without indices, you can create a tuple of consecutive pairs (res[0], args[0], ... , res[-1], args[-1]).

This is done by this idiom:

>>> sum(zip(res, args), ())
('Irene Adler', 10, 35, 5, 24.798, 0.2)

You can now pass this into a simplified format string (via *-argument):

>>> fmt = sum(zip(res, args), ())
>>> print('{:{}s} {:{}d} {:{}f}'.format(*fmt))
('Irene Adler', 10, 35, 5, 24.798, 0.2)

This can, of course, be done on one line:

>>> print('{:{}s} {:{}d} {:{}f}'.format(*sum(zip(res, args), ())))
Irene Adler    35 24.80

To make it readable, I would name the transformation:

def flat_pairs(iterable1, iterable2):
    return sum(zip(iterable1, iterable2), ())

# (...)

>>> print('{:{}s} {:{}d} {:{}f}'.format(*flat_pairs(res, args)))
Irene Adler    35 24.80

I think the last one is a reasonable trade-off between readability, convenience, and of course - showing off your Pythonic way of thinking, which is the primary motivation for playing with such stuff.



来源:https://stackoverflow.com/questions/10875121/using-format-to-format-a-list-with-field-width-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!