[Tutor] Re: print as columns

Michael P. Reilly arcege@speakeasy.net
Wed, 18 Jul 2001 09:18:42 -0400 (EDT)


Christopher Smith wrote
> So in a function you could have:
> 
> def pcolumns(...,*lists):
> 	n=len(max(lists,[]))
> 

No, read the previous postings.  The max function does not compare
the length of the sequences unless all data is the same (to the point
were the lengths are different).  The max and min functions use the
cmp function.

>>> max([7, 9], [2, 4, 5])
[7, 9]
>>> cmp([7, 9], [2, 4, 5])
1
>>> class A:
...   def __init__(self, i): self.i = i
...   def __str__(self): return str(self.i)
...   def __cmp__(self, other):
...     print '__cmp__(%s, %s)' % (self, other)
...     return cmp(self.i, other)
...   def __rcmp__(self, other):
...     print '__cmp__(%s, %s)' % (other, self)
...     return cmp(other, self.i)
...
>>> a = A(3)
>>> b = A(10)
>>> print max(a, b)
__cmp__(10, 3)
__cmp__(3, 10)
10
>>>

Compare the lengths, not the content.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |