打印表格的 Pythonic 方式

发布于 2024-08-08 02:30:11 字数 750 浏览 6 评论 0原文

我正在使用这个简单的函数:

def print_players(players):
    tot = 1
    for p in players:
        print '%2d: %15s \t (%d|%d) \t was: %s' % (tot, p['nick'], p['x'], p['y'], p['oldnick'])
        tot += 1

并且我假设昵称不超过 15 个字符。
我想保持每个“列”对齐,是否有一些语法糖允许我做同样的事情,但保持昵称列左对齐而不是右对齐,而不破坏右侧的列?

等效的、丑陋的代码是:

def print_players(players):
    tot = 1
    for p in players:
        print '%2d: %s \t (%d|%d) \t was: %s' % (tot, p['nick']+' '*(15-len(p['nick'])), p['x'], p['y'], p['oldnick'])
        tot += 1

感谢大家,这是最终版本:

def print_players(players):
    for tot, p in enumerate(players, start=1):
        print '%2d:'%tot, '%(nick)-12s (%(x)d|%(y)d) \t was %(oldnick)s'%p

I'm using this simple function:

def print_players(players):
    tot = 1
    for p in players:
        print '%2d: %15s \t (%d|%d) \t was: %s' % (tot, p['nick'], p['x'], p['y'], p['oldnick'])
        tot += 1

and I'm supposing nicks are no longer than 15 characters.
I'd like to keep each "column" aligned, is there a some syntactic sugar allowing me to do the same but keeping the nicknames column left-aligned instead of right-aligned, without breaking column on the right?

The equivalent, uglier, code would be:

def print_players(players):
    tot = 1
    for p in players:
        print '%2d: %s \t (%d|%d) \t was: %s' % (tot, p['nick']+' '*(15-len(p['nick'])), p['x'], p['y'], p['oldnick'])
        tot += 1

Thanks to all, here is the final version:

def print_players(players):
    for tot, p in enumerate(players, start=1):
        print '%2d:'%tot, '%(nick)-12s (%(x)d|%(y)d) \t was %(oldnick)s'%p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌上青苔 2024-08-15 02:30:11

要左对齐而不是右对齐,请使用 %-15s 而不是 %15s

To left-align instead of right-align, use %-15s instead of %15s.

总攻大人 2024-08-15 02:30:11

稍微偏离主题,但您可以使用 枚举

for tot, p in enumerate(players, start=1):
    print '...'

Slightly off topic, but you can avoid performing explicit addition on tot using enumerate:

for tot, p in enumerate(players, start=1):
    print '...'
乱世争霸 2024-08-15 02:30:11

或者,如果您使用 python 2.6,您可以使用 format 方法字符串:

这定义了一个值字典,并将它们用于显示:

>>> values = {'total':93, 'name':'john', 'x':33, 'y':993, 'oldname':'rodger'}
>>> '{total:2}: {name:15} \t ({x}|{y}\t was: {oldname}'.format(**values)
'93: john            \t (33|993\t was: rodger'

Or if your using python 2.6 you can use the format method of the string:

This defines a dictionary of values, and uses them for dipslay:

>>> values = {'total':93, 'name':'john', 'x':33, 'y':993, 'oldname':'rodger'}
>>> '{total:2}: {name:15} \t ({x}|{y}\t was: {oldname}'.format(**values)
'93: john            \t (33|993\t was: rodger'
回忆躺在深渊里 2024-08-15 02:30:11

看到 p 似乎是一个字典,怎么样:

print '%2d' % tot + ': %(nick)-15s \t (%(x)d|%(y)d) \t was: %(oldnick)15s' % p

Seeing that p seems to be a dict, how about:

print '%2d' % tot + ': %(nick)-15s \t (%(x)d|%(y)d) \t was: %(oldnick)15s' % p
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文