【问题标题】:Python/Format - List and widthPython/格式 - 列表和宽度
【发布时间】:2013-09-25 22:03:53
【问题描述】:

这就是我的格式到目前为止对我的列表的工作方式:

print('{:>15} {:>15} {:>15}{:>15}'.format(*i_liste_l))

我对python很陌生,我不知道如何通过动态设置动态宽度

len_total = int(len(str(mi_f)))
if len_total >= 15:
    width = int(len_total)
else: width = int(15)
print('{:>width} {:>width} {:>width}{:>width}'.format(*i_liste_l))

这可能吗?我是否必须为列表中的所有项目设置宽度?

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以指定宽度如下(传递关键字参数width):

    >>> '{:>{width}} {:>{width}}'.format(1, 2, width=5)
    '    1     2'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多