473,782 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16769
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********@yah oo.com> wrote in message
news:1d******** *************** ***@posting.goo gle.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?

Jul 18 '05 #5
dj********@yaho o.com (Jay Davis) wrote in message news:<1d******* *************** ****@posting.go ogle.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
4735
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
3261
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 pickle it? Thanks , Fedor
29
3410
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 wonder why it is not implemented for tuples. George
37
2393
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
388
21930
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
43
3384
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 this case, just zeros (though I just to prove whatever ends up working, having a counting generator would be nice). The target syntax would be something like >>> a,b,c = zeros() >>> q,r,s,t,u,v = zeros()
2
3486
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 questions are stimulated by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 Looking at that, what fails if I leave out the following line? ::
11
3556
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 IQueryable<TLessThan<T>(this IQueryable<Tsource, string property, object value); For example var q = db.Customers.LessThanOrEqual( "City", "London" );
6
8001
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(); Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list. Error 2: i cannot appear in a constant expression.
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.