我有一个列表,表示数据网格(在电子表格中考虑行).每行可以有任意数量的列,每个单元格中的数据都是任意长度的字符串.
我想规范化这一点,实际上使每行具有相同数量的列,并且数据中的每列具有相同的宽度,必要时用空格填充.例如,给出以下输入:
( ("row a","a1","a2","a3"),("another row","b1"),("c","x","y","a long string") )
我想要的数据如下所示:
( ("row a ","a3 "),"b1"," "," "),("c ","x ","y ","a long string") )
python 2.6或更高版本的pythonic解决方案是什么?只是为了清楚:我不想打印列表本身,我正在寻找一个解决方案,返回一个新的列表(或元组元组)与填充的值.
解决方法
从您的输入数据开始:
>>> d = ( ("row a","a long string") )
进行一次通过以确定每列的最大大小:
>>> col_size = {} >>> for row in d: for i,col in enumerate(row): col_size[i] = max(col_size.get(i,0),len(col)) >>> ncols = len(col_size)
然后进行第二遍,将每列填充到所需的宽度:
>>> result = [] >>> for row in d: row = list(row) + [''] * (ncols - len(row)) for i,col in enumerate(row): row[i] = col.ljust(col_size[i]) result.append(row)
这给出了期望的结果:
>>> from pprint import pprint >>> pprint(result) [['row a ','a1','a2','a3 '],['another row','b1',' ',' '],['c ','x ','y ','a long string']]
为了方便起见,这些步骤可以组合成一个功能:
def align(array): col_size = {} for row in array: for i,len(col)) ncols = len(col_size) result = [] for row in array: row = list(row) + [''] * (ncols - len(row)) for i,col in enumerate(row): row[i] = col.ljust(col_size[i]) result.append(row) return result