Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying a tuple by a scalar

I have the following code:

print(img.size) print(10 * img.size) 

This will print:

(70, 70) (70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70) 

I'd like it to print:

(700, 700) 

Is there any way to do this without having to write:

print(10 * img.size[0], 10 * img.size[1]) 

PS: img.size is a PIL image. I don't know if that matters anything in this case.

like image 515
devoured elysium Avatar asked Nov 23 '09 09:11

devoured elysium


People also ask

How do you multiply a tuple?

Concatenating and Multiplying Tuples Concatenation is done with the + operator, and multiplication is done with the * operator. Because the + operator can concatenate, it can be used to combine tuples to form a new tuple, though it cannot modify an existing tuple. The * operator can be used to multiply tuples.

How do you do scalar multiplication in Python?

In order to multiply array by scalar in python, you can use np. multiply() method.

Can you index a tuple?

Tuple Indexing We can access elements in a tuple in the same way as we do in lists and strings. Hence, we can access elements simply by indexing and slicing.

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.


2 Answers

Might be a nicer way, but this should work

tuple([10*x for x in img.size]) 
like image 131
Hannes Ovrén Avatar answered Oct 28 '22 01:10

Hannes Ovrén


img.size = tuple(i * 10 for i in img.size) 
like image 32
mthurlin Avatar answered Oct 27 '22 23:10

mthurlin