我使用這個簡單的功能:Python化的方式來打印表
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
**值是什麼意思?我只在參數聲明(函數/方法的聲明)中看到了'**'。 – 2009-10-22 09:36:01
@Andrea:它將字典的鍵/值對作爲一系列命名參數傳遞給函數。所以,如果'd = {'a':1,'b':2}',那麼'f(** d)'相當於'f(a = 1,b = 1)'。 – Stephan202 2009-10-22 09:41:51
我不知道,非常感謝! – 2009-10-22 09:43:12