Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: jt@toerring.de (Jens Thoms Toerring) Newsgroups: comp.lang.python Subject: Re: Sum operation in numpy arrays Date: 2 May 2013 10:24:13 GMT Organization: Freie Universitaet Berlin Lines: 42 Message-ID: References: <756832e9-4579-498e-b55c-f1a98f42a113@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de tkY12JENzeCTal7BJ3BV4w3mgzupvsupd/83kkjJ/wBi10 X-Orig-Path: not-for-mail User-Agent: tin/1.9.3-20080506 ("Dalintober") (UNIX) (Linux/2.6.30-1-amd64 (x86_64)) Xref: csiph.com comp.lang.python:44616 Ana DionĂ­sio wrote: > Hello! I have several numpy arrays in my script and i want to add them. For example: > a=[1,2,3,4,5] > b=[1,1,1,1,1] > c=[1,0,1,0,1] > for i in range(5): > d[i]=a[i]+b[i]+c[i] > print d > [3,3,5,5,7] > I did it like that but I get an error: "TypeError: unsupported operand type(s) for +: 'float' and 'numpy.string_'" > How can I sum my arrays? There's no numpy array at all in use in your example program, you only have normal lists. If you had numpy arrays you wouldn't need to loop over the indivindual elements, you could do just d = a + b + c to add up the (numpy) arrays. The error message you get may indicate that somewhere in your real code (not the one you posted) one of the elements of one of the (numpy) arrays is not a number but a string, e.g. by doing some- thing like a = numpy.array( [ 1, '2', 3, 4, 5 ] ) (note the single quotes around 2, that will result in the element having a type of 'numpy.string'. But if it's like that is impossible to tell without seeing the real code you're using;-) I guess you will have to give us what you actually use if you want something more helpful. Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de