473,396 Members | 1,936 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,396 software developers and data experts.

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 4713
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_sequence(sequence, multiplier):
new_sequence = map(lambda item: item*multiplier, sequence)
if type(sequence) == types.TupleType:
return tuple(new_sequence)
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.mul, 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
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 *...
2
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...
15
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...
4
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
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...
3
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....
0
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...
12
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...
1
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.