简体   繁体   English

如何在Python中打印部分嵌套列表?

[英]How to print part of a nested list in Python?

so if I have a 2d list like: 因此,如果我有一个二维列表,例如:

A =[['   ', '   ', '   '],['   ', '   ', '   '],['   ', '   ', '   '],['   ', '   ', '   ']]
col = 3

My code: 我的代码:

for i in A:
    print('|', end = '')
    print(*i, sep = '', end = '|\n')
print(' ' + 3* col*'-')

This will print the whole list: 这将打印整个列表:

How should I modify it so that it just print the last 3 rows 我应该如何修改它以便仅打印最后三行

To print the last three rows, which I assume means two rows with "sides" and one row at the bottom, change the line with the for statement to: 要打印最后三行(我假设这意味着两行带有“边”,而在底部一行),请使用for语句将行更改为:

for i in A[-2:]:

If you want the last three rows of A itself (in addition to the bottom row of dashes), then 如果您想要A本身的最后三行(除了最下面的破折号),那么

for i in A[-3:]:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM