我正在打开这个帖子,因为我想知道如何以及是否可以自定义列表输出:
1 2.33 KG
2 3.0 KG
3 (empty) 3
x 222 KG
y 233.4 KG
y 112 %
z 222 KG
w 9.98 KG
a 3224 KG
... .... ....
有人可以告诉我吗?
答案 0 :(得分:1)
根据您尝试的操作,您可以试用pandas,它非常适合处理和打印2d数组,或string formatting中讨论的this answer
答案 1 :(得分:0)
尝试在列表中构建列表。 像[[1,2,33,KG],[2,3.0,KG],[3,' ',3]] 然后,您可以通过迭代列表来访问每个元素。
<table>
for i in [[1 ,2.33 ,KG],[2, 3.0,KG],[3,' ',3]]:
<tr>
for j in i:
<td>j</td>
</tr>
</table>
答案 2 :(得分:0)
Please show your code. But I assume you want to know how to do what you've typed above.
Preferably, you'd want to place the variables in a list of tuples like this:
your_tuple_list_name = [(1, 2.33, 'KG'), (2, 3.0, 'KG'), (3, '(empty)', 'KG'), ('x', 222, 'KG'), ('y', 233.4, 'KG'), ('y', 112, '%'), ('z', 222, 'KG'), ('w', 9.98, 'KG'), ('a', 3224, 'KG'), ('.', '....', '..'), (add other tuples here)]`
If you want to add more items to the list of tuples just add them following the format:
(first_column, second_column, third_column)
And then the method would be like this:
def formatted_printing(your_tuple_list):
space = ' '
for n in range(len(your_tuple_list)):
number = 8 - len(str((your_tuple_list[n][1])))
print("{:>} {:<}{}{:<}".format(your_tuple_list[n][0], your_tuple_list[n][1], (space * number), your_tuple_list[n][2]))
formatted_printing(your_tuple_list_name)
With the following being the output:
1 2.33 KG
2 3.0 KG
3 (empty) KG
x 222 KG
y 233.4 KG
y 112 %
z 222 KG
w 9.98 KG
a 3224 KG
. .... ....