Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multiply tuples of equal length

I was hoping for an elegant or effective way to multiply sequences of integers (or floats).

My first thought was to try (1, 2, 3) * (1, 2, 2) would result (1, 4, 6), the products of the individual multiplications.

Though python isn't preset to do that for sequences. Which is fine, I wouldn't really expect it to. So what's the pythonic way to multiply (or possibly other arithmetic operations as well) each item in two series with and to their respective indices?

A second example (0.6, 3.5) * (4, 4) = (2.4, 14)

like image 596
ThorSummoner Avatar asked Feb 17 '15 02:02

ThorSummoner


People also ask

Can you multiply tuples in Python?

Concatenating and Multiplying Tuples Operators can be used to concatenate or multiply tuples. Concatenation is done with the + operator, and multiplication is done with the * operator.

How do you multiply all tuple elements in Python?

To multiply the elements of a tuple: Use a generator expression to iterate over the tuple. Multiply each tuple element by a number. Use the tuple() class to convert the result to a tuple.

How do you multiply two elements in a list Python?

Multiply Two Lists in Python Using the numpy. multiply() Method. The multiply() method of the NumPy library in Python, takes two arrays/lists as input and returns an array/list after performing element-wise multiplication.

Can we concatenate tuple?

When it is required to concatenate multiple tuples, the '+' operator can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error.

How to multiplicate tuple in Python?

Tuple multiplication in Python. When it is required to perform tuple multiplication, the 'zip' method and the generator expression can be used. The zip method takes iterables, aggregates them into a tuple, and returns it as the result. Generator is a simple way of creating iterators.

How to multiply two equal length lists in Python?

In python, to multiply two equal length lists we will use zip () to get the list and it will multiply together and then it will be appended to a new list.

How do you find the length of a tuple in Python?

Tuple Length To determine how many items a tuple has, use the len()method: Example Print the number of items in the tuple: thistuple = ("apple", "banana", "cherry") print(len(thistuple)) Try it Yourself »

How to multiply two arrays in Python?

In python, element-wise multiplication can be done by importing numpy. To multiply two equal-length arrays we will use np.multiply() and it will multiply element-wise. Example: import numpy as np m1 = [3, 5, 1] m2 = [2, 1, 6] print(np.multiply(m1, m2))


2 Answers

The simplest way is to use zip function, with a generator expression, like this

tuple(l * r for l, r in zip(left, right))

For example,

>>> tuple(l * r for l, r in zip((1, 2, 3), (1, 2, 3)))
(1, 4, 9)
>>> tuple(l * r for l, r in zip((0.6, 3.5), (4, 4)))
(2.4, 14.0)

In Python 2.x, zip returns a list of tuples. If you want to avoid creating the temporary list, you can use itertools.izip, like this

>>> from itertools import izip
>>> tuple(l * r for l, r in izip((1, 2, 3), (1, 2, 3)))
(1, 4, 9)
>>> tuple(l * r for l, r in izip((0.6, 3.5), (4, 4)))
(2.4, 14.0)

You can read more about the differences between zip and itertools.izip in this question.

like image 87
thefourtheye Avatar answered Nov 03 '22 07:11

thefourtheye


A simpler way would be:

from operator import mul

In [19]: tuple(map(mul, [0, 1, 2, 3], [10, 20, 30, 40]))
Out[19]: (0, 20, 60, 120)
like image 29
Victor Castillo Torres Avatar answered Nov 03 '22 07:11

Victor Castillo Torres