[Tutor] max(len(item)) for cols

Alan G alan.gauld at freenet.co.uk
Fri Jun 17 19:59:38 CEST 2005


I have a 2D array:
>>>[['1', '2 ', '3    '], ['longer     ', 'longer    ', 'sort']]

> How can I  get what is the max(len(item)) for the columns ?
> I would like to get a list with the max_col_width values.

>>> tda = [['1', '2 ', '3    '], ['longer     ', 'longer    ',
'sort']]
>>> mcw = [[len(r) for r in c] for c in tda]
>>> mcw
[[1, 2, 5], [11, 10, 4]]
>>> mcw = [max([len(r) for r in c]) for c in tda]
>>> mcw
[5, 11]
>>> widest = max(mcw)
>>> widest
11

Is that what you mean?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list