osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

TypeError: can't multiply sequence by non-int of type 'tuple'


On Friday, February 21, 2014 12:01:53 PM UTC+5:30, Jaydeep Patil wrote:
> HI,

> I have a tuple. I need to make sqaure of elements of tuple and after that i want add all suared tuple elements for total. When i trying to do it, below error came.

> Code:
> seriesxlist1 = ((0.0,), (0.01,), (0.02,), (0.03,), (0.04,), (0.05,), (0.06,), (0.07,), (0.08,), (0.09,), (0.1,), (0.11,))

> x2 = [x * x for x in seriesxlist1];

> Error:
> Traceback (most recent call last):
>     x2 = [x * x for x in seriesxlist1];
> TypeError: can't multiply sequence by non-int of type 'tuple'


> Please suggest me solution.

>>> x2 = [x * x for (x,) in seriesxlist1]
>>> x2
[0.0, 0.0001, 0.0004, 0.0009, 0.0016, 0.0025000000000000005, 0.0036, 0.004900000000000001, 0.0064, 0.0081, 0.010000000000000002, 0.0121]
>>> 

1 Why you are making singleton tuples dunno
2 And no need for semicolons in python