我有一个像
这样的清单02 SCR-KUN-LST-1.
03 SCR-KUN-LISTE GRID PAGED SIZE 145 LINES 21 3-D
LINE 1 COL 1,25 FONT SMALL-FONT
RECORD-DATA = KUN-LISTE-LINJE
DATA-COLUMNS = (1 11 12 62 112 142 157 172 187 237 252)
DISPLAY-COLUMNS = (1 10 12 40 65 86 99 112 125 135)
ALIGNMENT = ("L","C","L","L","L","L","L","L","L","L")
ROW-DIVIDERS = (1,1)
COLUMN-DIVIDERS = (1,1)
DIVIDER-COLOR = 32
CURSOR-COLOR = 80
HEADING-MENU-POPUP 63
HEADING-COLOR = 256
CURSOR-FRAME-WIDTH = -1
NUM-ROWS = 0
VPADDING = 50
VIRTUAL-WIDTH = 155
VSCROLL ADJUSTABLE-COLUMNS USE-TAB
BOXED BORDER-COLOR BLACK
REORDERING-COLUMNS SORTABLE-COLUMNS
COLUMN-HEADINGS CENTERED-HEADINGS TILED-HEADINGS
POP-UP MENU KUN-MENU
ID IS 209 BEFORE PROCEDURE IS VIS-KOMMENTAR
EVENT PROCEDURE KUN-LISTE-SAVE
EXCEPTION PROCEDURE KUN-LISTE-EVENTS.
如何打印指定列宽的列表
例如,我想打印mylist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
然后打印新行
column = 5
或者我想打印print(mylist, column= 5)
[ 1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20]
然后打印新行
column = 10
我知道我可以使用for循环来做到这一点,但我想知道是否有一个函数已经这样做了?
答案 0 :(得分:5)
使用numpy数组而不是列表并重塑数组。
ValueError
当然,如果无法将array
划分为column
同等大小的列,则会抛出{{1}}。
答案 1 :(得分:3)
不确定为什么,但我认为接近我认为你想要实现的东西可以通过将行数固定为-1来使用numpy数组重塑来完成
import numpy as np
array=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]) array
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20])
array.reshape(-1,5)
给出
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
array.reshape(-1,10)
给出
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
答案 2 :(得分:1)
您也可以使用切片来完成此操作。
mylist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
def print_list(mylist, no_of_cols):
start_index = 0
for i in range(no_of_cols, len(mylist), no_of_cols):
print mylist[start_index:i]
start_index = i
if len(mylist) > start_index:
print mylist[start_index:len(mylist)]
print_list(mylist, 5)