473,792 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiply utple or list

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
Jul 18 '05 #1
4 4736
Jim Red wrote:
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


i.e.:
import operator
[ operator.mul(x, 2) for x in (4,5)]


regards,
Karl

Jul 18 '05 #2
P
Jim Red wrote:
what is the best way to multiply a tuple or list by a given value

exp.
multiply (4, 5) by 2 => (8, 10)


something like this?

import types
def multiply_sequen ce(sequence, multiplier):
new_sequence = map(lambda item: item*multiplier , sequence)
if type(sequence) == types.TupleType :
return tuple(new_seque nce)
elif type(sequence) == types.ListType:
return new_sequence

--
Pádraig Brady - http://www.pixelbeat.org

Jul 18 '05 #3
> >>> import operator
>>> [ operator.mul(x, 2) for x in (4,5)]


Why not simply * ?

[ x * 2 for x in (4,5)]

And you might surround it by tuple to gain a tuple

tuple([ x * 2 for x in (4,5)])

Another way would be to use Numeric

import Numeric
a = Numeric.array(( 10,2), Numeric.Float32 )
a = a * 2
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
Diez B. Roggisch wrote:
>>> import operator
>>> [ operator.mul(x, 2) for x in (4,5)]

Why not simply * ?

[ x * 2 for x in (4,5)]


oops, of course. I had the map function in mind,
which only accepts a function, but this would come
to something like:

li=(4,5)
map(operator.mu l, li, (2,)*len(li))

and is certainly worse then list comprehension :-)

regards,
Karl

Jul 18 '05 #5

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

Similar topics

5
16772
by: Jay Davis | last post by:
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?
2
3097
by: Martin Pettersson | last post by:
Hi all, I'm trying to multiply parent values in xsl. The thing is that I start with a value down in the xml-structure. From that value (in my case 'qty' value) I check the parent value and later on I will try to multiply these values. When I have found the parent I will multiply with its parent and so on, all the way to the top of the xml-file. The files are pasted below. First I have tried to find these values, the next step will be...
15
13281
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of matricies at compile-time? Any help would be appreciated. Hopefully this code comes in useful for someone, let me know if you find it useful, or if you have suggestions on how to improve it. // Public Domain by Christopher Diggins, 2005 ...
4
7248
by: trint | last post by:
Why do we have to use decimal.multiply to multiply two intgers? I just want to multiply these two values: int oneThousand = 1000; int watCHTimer1 = value from a textbox; Thanks, Trint
2
1537
by: slitchfield | last post by:
Sorry if this is real beginner's stuff, but I'm an old-school HTML guy and this is all another world to me. Is there an easy way to get the user to specify a number from 1 to 5 (in a pick list or radio button or similar) and get a javascript routine to multiply it by another hard-coded number (e.g. 7) and display the result further down the page? Thanks, Steve Litchfield
3
1863
by: noro | last post by:
Hi all, I use a method that accept multiply arguments ("plot(*args)"). so plot() is accepted and plot(,) is also accepted. the problem is that i know the number of arguments only at runtime. Let say that during runtime i need to pass 4 arguments, each is a list, creating a set of lists and passing it to the method wont word since the interpartor thinks it is only 1 argument the contain a reference to
0
4001
by: komandjaja | last post by:
Hi everybody. I am new here and I don't know much about CSS. However, I have a blog at multiply http://komandjaja.multiply.com and I have customized the CSS codes there to make a new theme. When I see my page thru Firefox, everything is okay but when I see it with IE, there are distorted things and some pictures don't show up. My platform is Windows XP home edition. Here's my css code to the site /************* Spiderman 3...
12
2095
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me with the following: Can this create any problems with GNU g++ compilation, linking etc? What would be the impact to the performance of the system? Anything else we have to consider?
1
1985
by: Gavin Chen | last post by:
Hello: I tried to install Tk800.015 on SunOS 4.1.4 with perl 5.6.2. At "make test" time, I got the error message as below: collect2: ld returned 2 exit status ld: /usr/local/lib/perl5/site_perl/5.6.2/sun4-sunos/auto/Tk/Event/Event.a(Event.o): _LangExit: multiply defined ld: /usr/local/lib/perl5/site_perl/5.6.2/sun4-sunos/auto/Tk/Event/Event.a(Event.o): _Tcl_Free: multiply defined ld:...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
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
10159
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
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4111
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
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.