[Tutor] Re: print as columns
Christopher Smith
csmith@blakeschool.org
Wed, 18 Jul 2001 10:39:42 -0500
arcege@speakeasy.net writes:
> Compare the lengths, not the content.
Thanks for persisting in pointing out the error. I had a bad
test case. I now see that len(max.. is wrong:
>>> a=[1]*3
>>> b=[7]
>>> len(max(a,b))
1
Whereas what you said is right:
>>> max(map(len,(a,b)))
3
Which works on the pathological case as long as you add a
dummy list--or just a comma in the tuple of lists (b,)
instead of (b):
>>> max(map(len,(b,)))
1
If you forget the comma, you may get this:
>>> max(map(len,(b)))
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: len() of unsized object
Finally, max applies itself iteratively so reduce isn't necessary
but it works, too.
>>> reduce(max,map(len,(b,)))
1
I wrote:
> So in a function you could have:
>
> def pcolumns(...,*lists):
> n=len(max(lists,[]))
>
Uh-humm, I should have said:
n=max(map(len,(lists,)))
Thanks again!
/c