假设有一个元组test = (1,2)
,我想计算10**test
它应该等于(10,100)
。但是Python给出了错误 unsupported operand type(s) for ** or pow(): 'int' and 'tuple'
如何在 python 中执行此操作?
Numpy 数组提供以下功能:
>>> import numpy as np
>>> test = np.array((1,2))
>>> test
array([1, 2])
>>> 10**test
array([ 10, 100])
如果你想使用普通的旧元组,你必须自己编写循环:
>>> test = (1,2)
>>> tuple(10**k for k in test)
(10, 100)