470,614 Members | 1,572 Online

# dividing tuple elements with an int or float

hi
i am trying to resize some images.First i'd read the size as a 2
tuple and then i want to divide it by 2 or 4 or 2.5 etc..

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 ..
thanks
RG
Mar 20 '08 #1
8 15567
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)

--
Steven
Mar 20 '08 #2
On Thu, Mar 20, 2008 at 3:42 AM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
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)

--
Jerry
Mar 20 '08 #3
On Mar 20, 9:28*am, "Jerry Hill" <malaclyp...@gmail.comwrote:
On Thu, Mar 20, 2008 at 3:42 AM, Steven D'Aprano

<st...@remove-this-cybersource.com.auwrote:
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.
"Diagram A" appends 0 to every item of a list.
>>y= [ [] for k in range( 10 ) ]
def x( fun, *ar, **kw ):
... def post( *xr, **xw ):
... return fun( *(xr+ ar), **kw )
... return post
...
>>x( list.append, 0 )( y[0] )
y
[[0], [], [], [], [], [], [], [], [], []]
>>x( list.pop, 0 )( y[0] )
0
>>y
[[], [], [], [], [], [], [], [], [], []]
>>list( map( x( list.append, 0 ), y ) )
[None, None, None, None, None, None, None, None, None, None]
>>y
[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]

If the elements are immutable,
>>y= [ () for k in range( 10 ) ]
list( map( x( tuple.__add__, ( 0, ) ), y ) )
[(0,), (0,), (0,), (0,), (0,), (0,), (0,), (0,), (0,), (0,)]

you get your new list from map. Here it is with integer elements:
>>y= ( 300, 400 )
tuple( map( x( int.__add__, 1, ), y ) )
(301, 401)

--You could spell the call like this:

With mul:
>>tuple( map( x( int.__mul__, 2, ), y ) )
(600, 800)

It's like partial, but the argument addition is commuted. The key is
that 'y' is going in the first slot, as 'self'. It's not clear that
argument addition commution applies (matches, is correct, generalizes
with right meaning): you just want the first parameter to come from a
separate call. Other uses cases of the generalizer might not align.
+ a fraction on 'x'.

Floats get harder, since you're looking at composition.
>>tuple( map( compose( float, x( float.__mul__, 1/ 2.5 ) ), y ) )
(120.0, 160.0) # (160, 120)

+1 compose.

def compose( *f ):
def post( *ar, **kw ):
if len( f )1:
return f[0]( compose( *f[1:] )( *ar, **kw ) )
return f[0]( *ar, **kw )
return post

Mar 21 '08 #4
On Mar 21, 8:14*am, castiro...@gmail.com wrote:
On Mar 20, 9:28*am, "Jerry Hill" <malaclyp...@gmail.comwrote:
On Thu, Mar 20, 2008 at 3:42 AM, Steven D'Aprano
<st...@remove-this-cybersource.com.auwrote:
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.
Mar 21 '08 #5
On 21 mar, 10:14, castiro...@gmail.com wrote:
On Mar 20, 9:28*am, "Jerry Hill" <malaclyp...@gmail.comwrote:
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.
"Diagram A" appends 0 to every item of a list.
>y= [ [] for k in range( 10 ) ]
def x( fun, *ar, **kw ):

... * * def post( *xr, **xw ):
... * * * * * * return fun( *(xr+ ar), **kw )
... * * return post
...>>x( list.append, 0 )( y[0] )
Sane programmers replace that crazyness with this code:

for x in collection:
x.append(0)
>y= ( 300, 400 )
tuple( map( x( int.__add__, 1, ), y ) )

(301, 401)
Sane programmers replace that crazyness with this code:

tuple(x+1 for x in y)
Floats get harder, since you're looking at composition.
>tuple( map( compose( float, x( float.__mul__, 1/ 2.5 ) ), y ) )

(120.0, 160.0) # (160, 120)
Sane programmers -like D'Aprano, Jerry Hill and me- replace that
crazyness with this code:

tuple(x/2.5 for x in y)
def compose( *f ):
* * def post( *ar, **kw ):
* * * * * * if len( f )1:
* * * * * * * * * * return f[0]( compose( *f[1:] )( *ar, **kw ) )
* * * * * * return f[0]( *ar, **kw )
* * return post
Sane programmers don't write such semi-functional things (unless it
helps expressing the problem in certain domains).
I now think that deprecating map, lambda & Co. was a good thing after
all.

--
Gabriel Genellina
Mar 21 '08 #6
On Mar 21, 1:08*pm, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
Sane programmers replace that crazyness with this code:

tuple(x+1 for x in y)

Sane programmers -like D'Aprano, Jerry Hill and me- replace that
crazyness with this code:

tuple(x/2.5 for x in y)

Sane programmers don't write such semi-functional things (unless it
helps expressing the problem in certain domains).
I now think that deprecating map, lambda & Co. was a good thing after
all.
If you write it that way the first time, you need therapy. Actually,
at this point, I (for one, personally) want to investigate 'certain
domains'. Tell me it's really bad at everything or what it's good
at. What can I respect about it?
Mar 22 '08 #7
On Mar 22, 2:23*pm, castiro...@gmail.com wrote:
Sane programmers don't write such semi-functional things (unless it
helps expressing the problem in certain domains).
I now think that deprecating map, lambda & Co. was a good thing after
all.

If you write it that way the first time, you need therapy. *Actually,
at this point, I (for one, personally) want to investigate 'certain
domains'. *Tell me it's really bad at everything or what it's good
at. *What can I respect about it?
If you (castiro..) write it your way, you'll surely win the Obfuscated
Python Code Contest.
Mar 23 '08 #8
On Mar 23, 12:22*pm, Lie <Lie.1...@gmail.comwrote:
On Mar 22, 2:23*pm, castiro...@gmail.com wrote:
Sane programmers don't write such semi-functional things (unless it
helps expressing the problem in certain domains).
I now think that deprecating map, lambda & Co. was a good thing after
all.
If you write it that way the first time, you need therapy. *Actually,
at this point, I (for one, personally) want to investigate 'certain
domains'. *Tell me it's really bad at everything or what it's good
at. *What can I respect about it?

If you (castiro..) write it your way, you'll surely win the Obfuscated
Python Code Contest.
Sometimes you're on a roll and you don't want to back up a step. What
are good "on a roll" habits? (Or, what roll?)
Mar 24 '08 #9

### This discussion thread is closed

Replies have been disabled for this discussion.