Multiplying a tuple by a scalar

后端 未结 12 883
忘了有多久
忘了有多久 2020-12-14 14:48

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,          


        
12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 15:13

    Solution:

    set1=(70, 70)
    tuple(2*array(set1))
    

    Explanation: arrays make direct scalar multiplication possible. Hence the tuple called set1 here is converted to an array. I assume you wish to keep using the tuple, hence we convert the array back to a tuple.

    This solution is to avoid the explicit and verbose for loop. I do not know whether it is faster or whether the exact same thing happens in both cases.

提交回复
热议问题