首页 > 解决方案 > python3.x 使用 format() 打印列表的每个值

问题描述

.format()我对以下功能有疑问python 3.6

基本上我有一个值列表(val_output_list),它已经按正确的顺序排列,然后我想打印出这样的格式,其中 V 是列表的值:

+-------+-------+-------+
| V V V | V V V | V V V |
| V V V | V V V | V V V |
| V V V | V V V | V V V |
+-------+-------+-------+
| V V V | V V V | V V V |
| V V V | V V V | V V V |
| V V V | V V V | V V V |
+-------+-------+-------+
| V V V | V V V | V V V |
| V V V | V V V | V V V |
| V V V | V V V | V V V |
+-------+-------+-------+

val_output_list 的输入是:

val_output_list = [6, 1, 4, 2, 3, 9, 8, 7, 5, 3, 8, 2, 5, 7, 6, 1, 9, 4, 9, 5, 7, 8, 1, 4, 3, 6, 2, 5, 7, 1, 3, 8, 2, 9, 4, 6, 8, 4, 6, 7, 9, 1, 5, 2, 3, 2, 3, 9, 6, 4, 5, 7, 8, 1, 1, 9, 5, 4, 6, 8, 2, 3, 7, 4, 2, 3, 9, 5, 7, 6, 1, 8, 7, 6, 8, 1, 2, 3, 4, 5, 9]

到目前为止我编写的代码如下:

print("+-------+-------+-------+\n")
for index, value in enumerate(val_output_list):
    print(("|" + " {} {} {} |"*2 + "{} {} {} |\n").format(val_output_list[value]))
    if index + 1 == 28:
        print("+-------+-------+-------+\n")
    elif index + 1 == 55:
        print("+-------+-------+-------+\n")
    elif index + 1 == 82:
        print("+-------+-------+-------+\n")

它背后的想法是我枚举列表中的值并使用索引来确定是否必须打印分隔线。然后我遍历值列表并打印占位符所在的值。但是我得到一个我不明白的例外

print(("|" + " {} {} {} |"*2 + "{} {} {} |\n").format(val_output_list[value]))
IndexError: tuple index out of range

标签: pythonpython-3.xformatting

解决方案


你可以使用这个:

matrix = [ str(r)+str(c) for r in range(9) for c in range(9)]

row_pattern = "| {} {} {} | {} {} {} | {} {} {} |"
row_len = len(row_pattern)  
row_line = "-" * row_len 
rp = row_pattern + "\n"
rl = row_line + "\n"

# build the full pattern
full_pattern = rl + rp * 3 + rl + rp * 3 + rl + rp * 3 + rl

print(full_pattern.format(*matrix))

输出:

----------------------------------
| 00 01 02 | 03 04 05 | 06 07 08 |
| 10 11 12 | 13 14 15 | 16 17 18 |
| 20 21 22 | 23 24 25 | 26 27 28 |
----------------------------------
| 30 31 32 | 33 34 35 | 36 37 38 |
| 40 41 42 | 43 44 45 | 46 47 48 |
| 50 51 52 | 53 54 55 | 56 57 58 |
----------------------------------
| 60 61 62 | 63 64 65 | 66 67 68 |
| 70 71 72 | 73 74 75 | 76 77 78 |
| 80 81 82 | 83 84 85 | 86 87 88 |
----------------------------------

matrix看起来像这样:

['00', '01', '02', '03', '04', '05', '06', '07', '08', 
 '10', '11', '12', '13', '14', '15', '16', '17', '18', 
 '20', '21', '22', '23', '24', '25', '26', '27', '28', 
 '30', '31', '32', '33', '34', '35', '36', '37', '38', 
 '40', '41', '42', '43', '44', '45', '46', '47', '48', 
 '50', '51', '52', '53', '54', '55', '56', '57', '58', 
 '60', '61', '62', '63', '64', '65', '66', '67', '68', 
 '70', '71', '72', '73', '74', '75', '76', '77', '78', 
 '80', '81', '82', '83', '84', '85', '86', '87', '88']

如果您有行列表,则可以使用列表解包:

matrix = [ [str(r)+str(c) for r in range(9)] for c in range(9)]

print(full_pattern.format(*(x for y in matrix for x in y)))

matrix看起来像:

[['00', '10', '20', '30', '40', '50', '60', '70', '80'], 
 ['01', '11', '21', '31', '41', '51', '61', '71', '81'], 
 ['02', '12', '22', '32', '42', '52', '62', '72', '82'], 
 ['03', '13', '23', '33', '43', '53', '63', '73', '83'], 
 ['04', '14', '24', '34', '44', '54', '64', '74', '84'], 
 ['05', '15', '25', '35', '45', '55', '65', '75', '85'], 
 ['06', '16', '26', '36', '46', '56', '66', '76', '86'], 
 ['07', '17', '27', '37', '47', '57', '67', '77', '87'], 
 ['08', '18', '28', '38', '48', '58', '68', '78', '88']]

推荐阅读