473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Multiply a tuple by a constant

I often need to multiply a tuple by a
constant and the methods I use now are
not very pretty. The idea is to get a
new tuple that is some factor times the
start tuple, like 'newtup = .5 * oldtup'.

Is there a slick way to do this?
Jul 18 '05 #1
5 16587
Jay Davis wrote:
I often need to multiply a tuple by a
constant and the methods I use now are
not very pretty. The idea is to get a
new tuple that is some factor times the
start tuple, like 'newtup = .5 * oldtup'.

Is there a slick way to do this?


Not really slick, but it works:
tuple(map(5 .__mul__, (1,2,3))) (5, 10, 15)

The space after the 5 is necessary. Alternatively, you can wrap the constant
in brackets:
tuple(map((5).__mul__, (1,2,3)))

(5, 10, 15)

Peter

Jul 18 '05 #2
These days list comprehensions are preferred:
tuple([5*i for i in (1,2,3)])

(5, 10, 15)
Jul 18 '05 #3
On Fri, 13 Feb 2004 02:20:06 -0800, Jay Davis wrote:
I often need to multiply a tuple by a constant and the methods I use now
are not very pretty. The idea is to get a new tuple that is some factor
times the start tuple, like 'newtup = .5 * oldtup'.

Is there a slick way to do this?


I suggest looking at Numarray - a very convenient way of doing maths on
sets of numbers. Of course if you don't do very much it's not worth doing
this.

http://www.stsci.edu/resources/softw...dware/numarray

You can do things like:

import numarray

a = numarray.array( (1.,2.,5.,-46.) )
a *= 0.5
b = a + 3.4

Jeremy

Jul 18 '05 #4
two ways:

t=(1,2,3)
newt=tuple(map(lambda x: x*0.5, t))

Note: apparently the map function is slated for deprication
(to be replaced by list comprehension)

List comprehension:

t=(1,2,3)
newt=tuple([x*0.5 for x in t])

-Larry Bates
-----------------------------------------------------------
"Jay Davis" <dj********@yahoo.com> wrote in message
news:1d**************************@posting.google.c om...
I often need to multiply a tuple by a
constant and the methods I use now are
not very pretty. The idea is to get a
new tuple that is some factor times the
start tuple, like 'newtup = .5 * oldtup'.

Is there a slick way to do this?

Jul 18 '05 #5
dj********@yahoo.com (Jay Davis) wrote in message news:<1d**************************@posting.google. com>...
I often need to multiply a tuple by a
constant and the methods I use now are
not very pretty. The idea is to get a
new tuple that is some factor times the
start tuple, like 'newtup = .5 * oldtup'.

Is there a slick way to do this?


You can always subclass tuple and redefine "*". Dunno if you would
consider it slick enough.

Michele
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim Red | last post by:
what is the best way to multiply a tuple or list by a given value exp. multiply (4, 5) by 2 => (8, 10) cheers jr
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
29
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I...
37
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
6
by: nielsp | last post by:
Hello! How can I iterate over a tuple (using C++0x)? I tried the following, but that doesn't work: for(int i=0; i<std::tuple_size<T...>::value; ++i) std::get<i>(my_tuple).do_sth(); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.