On 5/14/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Message: 3 > Date: Mon, 14 May 2007 14:21:50 -0400 > From: "David M. Cooke" <[EMAIL PROTECTED]> > Subject: Re: [Numpy-discussion] .max() and zero length arrays > To: Discussion of Numerical Python <numpy-discussion@scipy.org> > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=us-ascii > > On Mon, May 14, 2007 at 12:38:58PM -0500, Nick Fotopoulos wrote: > > Dear all, > > > > I find myself frequently wanting to take the max of an array that > > might have zero length. If it is zero length, it throws an exception, > > when I would like to gracefully substitute my own value. For example, > > one solution with lists is to do max(possibly_empty_list + > > [minimum_value]), but it seems clunky to do a similar trick with > > arrays and concatenate or to use a try: except: block. What do other > > people do? If there's no good idiom, would it be possible to add > > kwargs like default_value and/or minimum_value? > > What about if maximum returned negative infinity (for floats) > or the minimum int? That would make maximum act like sum and product, > where the identity for those functions is returned: > > In [2]: sum([]) > Out[2]: 0.0 > In [3]: product([]) > Out[3]: 1.0
I have mixed feelings. On the one hand, my new idiom would be: max(possidly_zero_len_array.max(), min_val) , which makes me happy. On the other hand: possibly_zero_len_array.max() in possibly_zero_len_array is no longer guaranteed to evaluate True, which me cringe a little, but might not bother others. That would, however, be an excellent behavior for a new supremum method or for a new boolean supremum kwarg in max. Further thoughts, David? Others? Thanks, Nick _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion