dividing tuple elements with an int or float

castironpi at gmail.com castironpi at gmail.com
Fri Mar 21 09:58:16 EDT 2008


On Mar 21, 8:14 am, castiro... at gmail.com wrote:
> On Mar 20, 9:28 am, "Jerry Hill" <malaclyp... at gmail.com> wrote:
> > On Thu, Mar 20, 2008 at 3:42 AM, Steven D'Aprano
> > <st... at remove-this-cybersource.com.au> wrote:
> > > On Wed, 19 Mar 2008 23:06:44 -0700, royG wrote:
>
> > >  > suppose
> > >  > origsz=(400,300)
> > >  > i want to divide the origsize by 2.5 so i can resize to (160,120)
>
> > >  > scale=2.5
> > >  > how can i get the newsz?
> > >  > obviously origsz/2.5 won't work  ..
>
> > >  newsz = (origsz[0]/scale, origsz[1]/scale)
>
> > That works fine for a 2-tuple, but might get unwieldy for larger
> > tuples, or if you don't know the length until runtime.  A more general
> > solution might use a generator expression, like this:
>
> > newsz = tuple(x/scale for x in origsz)
>
> You want to perform a uniform call on the elements of a collection.
> +1 compose.

By the way.  -And sorry for interrupting the OP's time-  I feel that
control flow objects could be a really powerful addition to a
language.  Compose a for-loop.



More information about the Python-list mailing list