Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68332

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

Newsgroups comp.lang.python
Date 2014-03-13 07:52 -0700
References <e84de2c2-5333-42ae-ae22-cf628c177ae1@googlegroups.com>
Message-ID <bdcc37eb-d7ca-42c7-aa0e-9a317d366dae@googlegroups.com> (permalink)
Subject Re: TypeError: can't multiply sequence by non-int of type 'tuple'
From simiasaro@gmail.com

Show all headers | View raw


On Friday, February 21, 2014 1:31:53 AM UTC-5, 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):
> 
>   File "<pyshell#188>", line 1, in <module>
> 
>     x2 = [x * x for x in seriesxlist1];
> 
> TypeError: can't multiply sequence by non-int of type 'tuple'
> 
> 
> 
> 
> 
> 
> 
> Please suggest me solution.

You may want to identify which of the tuple value you want to multiply, especially since the tuple has only one value:
x2 = [x[0] * x[0] for x in seriesxlist1]

Then, you can add the values in the list:
sum(x2)

or the quickest way:
sum(x[0] * x[0] for x in seriesxlist1)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: TypeError: can't multiply sequence by non-int of type 'tuple' simiasaro@gmail.com - 2014-03-13 07:52 -0700

csiph-web