Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44616
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Sum operation in numpy arrays |
| Date | 2013-05-02 10:24 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <auet6dFmjutU1@mid.uni-berlin.de> (permalink) |
| References | <756832e9-4579-498e-b55c-f1a98f42a113@googlegroups.com> |
Ana Dionísio <anadionisio257@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Sum operation in numpy arrays Ana Dionísio <anadionisio257@gmail.com> - 2013-05-02 02:10 -0700 Re: Sum operation in numpy arrays jt@toerring.de (Jens Thoms Toerring) - 2013-05-02 10:24 +0000 Re: Sum operation in numpy arrays Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-02 10:33 +0000
csiph-web