0

我有以下代码,它基本上返回一个player1Hand包含 5 个列表的列表 ()(在这种情况下,是关于所述手牌的个人信息)。

def getPlayer2Hand(handAmount):
    getPlayer2Deck()
    player2Hand = []

    for i in range(handAmount):
        card = []
        card.append(deckListPlayer2Full[i]['NameOfCard'])
        card.append(deckListPlayer2Full[i]['Attack'])
        card.append(deckListPlayer2Full[i]['Defense'])
        card.append(deckListPlayer2Full[i]['Magic'])
        card.append(deckListPlayer2Full[i]['Shield'])
        player2Hand.append(card)

    return player2Hand

如何将列表中的信息“打印player2Hand"到可读的列或表格中?我想使用上面的 dict 键(NameOfCard,Attack等)作为标题。

我尝试使用 TextTable,但我似乎无法让它工作。

这是一个player2Hand看起来像的示例:

[['Knight', '500', '500', '0', '0'], 
['Mage', '0', '0', '500', '500'], 
['Mage', '0', '0', '500', '500'], 
['Mage', '0', '0', '500', '500'], 
['Mage', '0', '0', '500', '500']]
4

2 回答 2

1

位字符串格式可能会有所帮助:

>>> data = [['Knight', '500', '500', '0', '0'],
            ['Mage', '0', '0', '500', '500'],
            ['Mage', '0', '0', '500', '500'],
            ['Mage', '0', '0', '500', '500'],
            ['Mage', '0', '0', '500', '500']]

>>> frmt = '{:10s}' + 4 * '{:>12s}'
>>> for line in data::
        print(frmt.format(*line))

结果是:

Knight             500         500           0           0
Mage                 0           0         500         500
Mage                 0           0         500         500
Mage                 0           0         500         500
Mage                 0           0         500         500
于 2013-05-28T01:29:06.047 回答
0

只是快速而肮脏地刺穿您可能想要做的事情:

>>> x = [['Knight', '500', '500', '0', '0'],['Mage', '0', '0', '500', '500'], ['Mage', '0', '0', '500', '500'], ['Mage', '0', '0', '500', '500'],  ['Mage', '0', '0', '500', '500']]
>>> for row in x:
...     output = [row[0].ljust(20)]
...     for col in row[1:]:
...         output.append(col.rjust(10))
...     print ''.join(output)
... 
Knight                     500       500         0         0
Mage                         0         0       500       500
Mage                         0         0       500       500
Mage                         0         0       500       500
Mage                         0         0       500       500
于 2013-05-28T00:59:09.970 回答