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

Kent Johnson kent37 at tds.net
Fri Jun 17 13:43:48 CEST 2005


Max Noel wrote:
> On Jun 17, 2005, at 11:58, János Juhász wrote:
>>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.
>
>  >>> a = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1]]
>  >>> max(len(item) for item in a)
> 4
> 
>      If you're running Python 2.3 or lower, add brackets in the last  
> line:
>  >>> max([len(item) for item in a])
> 4

another way:
 >>> a = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1]]
 >>> max(map(len, a))
4



More information about the Tutor mailing list