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


Groups > comp.lang.python > #44102 > unrolled thread

Lists and arrays

Started byAna Dionísio <anadionisio257@gmail.com>
First post2013-04-22 11:13 -0700
Last post2013-04-23 19:30 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Lists and arrays Ana Dionísio <anadionisio257@gmail.com> - 2013-04-22 11:13 -0700
    Re: Lists and arrays Dave Angel <davea@davea.name> - 2013-04-22 14:34 -0400
    Re: Lists and arrays "BartC" <bc@freeuk.com> - 2013-04-22 22:24 +0100
    Re: Lists and arrays 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-23 00:50 -0700
    Re: Lists and arrays Denis McMahon <denismfmcmahon@gmail.com> - 2013-04-23 19:30 +0000

#44102 — Lists and arrays

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-04-22 11:13 -0700
SubjectLists and arrays
Message-ID<de1cc79e-cbf7-4b0b-ae8e-18841a1ef095@googlegroups.com>
Hello!

I need your help!

I have an array and I need pick some data from that array and put it in a list, for example:

array= [a,b,c,1,2,3]

list=array[0]+ array[3]+ array[4]

list: [a,1,2]

When I do it like this: list=array[0]+ array[3]+ array[4] I get an error:

"TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'"

Can you help me?

[toc] | [next] | [standalone]


#44103

FromDave Angel <davea@davea.name>
Date2013-04-22 14:34 -0400
Message-ID<mailman.934.1366655702.3114.python-list@python.org>
In reply to#44102
On 04/22/2013 02:13 PM, Ana Dionísio wrote:
> Hello!
>
> I need your help!
>
> I have an array

I think you mean you have a numpy array, which is very different than a 
python array.array


> and I need pick some data from that array and put it in a list, for example:
>
> array= [a,b,c,1,2,3]

That's a list.

>
> list=array[0]+ array[3]+ array[4]

Nothing wrong with that, other than that you just hid the name of the 
list type, making it tricky to later convert things to lists.

>
> list: [a,1,2]

You'll never get that.  When you assign an object to a list, the object 
itself is referenced in that list, not the name that it happened to have 
before.  So if a was an object of type float and value 41.5, then you 
presumably want:
    mylist: [41.5, 1, 2]


>
> When I do it like this: list=array[0]+ array[3]+ array[4] I get an error:
>
> "TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'"
>

Apparently you did not use the line
     array= [a,b,c,1,2,3]

as you said above, but some other assignment, perhaps using a numpy 
method or six.  Worse, apparently the elements of that collection aren't 
simple numbers but some kind of numpy thingies as well.

If you show what you actually did, probably someone here can help, 
though the more numpy you use, the less likely that it'll be me.


If you really had a list, you wouldn't have gotten an error, but neither 
would you have gotten anything like you're asking.  array[3] + array[4] 
== 1+2 == 3.  If you're trying to make a list using + from a subscripted 
list, you'd have to enclose each integer in square brackets.

mylist = [array[0]] + [array[3]] + [array[4]]

Alternatively, you could just do

mylist = [ array[0], array[3], array[4] ]

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#44115

From"BartC" <bc@freeuk.com>
Date2013-04-22 22:24 +0100
Message-ID<JVhdt.49368$8L4.10144@fx29.fr7>
In reply to#44102

"Ana Dionísio" <anadionisio257@gmail.com> wrote in message 
news:de1cc79e-cbf7-4b0b-ae8e-18841a1ef095@googlegroups.com...
> Hello!
>
> I need your help!
>
> I have an array and I need pick some data from that array and put it in a 
> list, for example:
>
> array= [a,b,c,1,2,3]
>
> list=array[0]+ array[3]+ array[4]
>
> list: [a,1,2]
>
> When I do it like this: list=array[0]+ array[3]+ array[4] I get an error:
>
> "TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 
> 'numpy.ndarray'"

 You're calculating a+1+2. Probably a isn't something that can be added to 
1+2.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#44164

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-04-23 00:50 -0700
Message-ID<155ef657-aaff-4b31-aa55-ba76cdd179d7@googlegroups.com>
In reply to#44102
Ana Dionísio於 2013年4月23日星期二UTC+8上午2時13分38秒寫道:
> Hello!
> 
> 
> 
> I need your help!
> 
> 
> 
> I have an array and I need pick some data from that array and put it in a list, for example:
> 
> 
> 
> array= [a,b,c,1,2,3]
> 
> 
> 
> list=array[0]+ array[3]+ array[4]
> 
> 
> 
> list: [a,1,2]
> 
> 
> 
> When I do it like this: list=array[0]+ array[3]+ array[4] I get an error:
> 
> 
> 
> "TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'"
> 
> 
> 
> Can you help me?

The list part in Python is more versatile but definitely
executed slower than the array in C.

What I like is that maintaining the Python part
is not as tedious and painful for the same programs in LISP.



 

[toc] | [prev] | [next] | [standalone]


#44209

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-04-23 19:30 +0000
Message-ID<kl6nfq$d1m$2@dont-email.me>
In reply to#44102
On Mon, 22 Apr 2013 11:13:38 -0700, Ana Dionísio wrote:

> I have an array and I need pick some data from that array and put it in
> a list, for example:
> 
> array= [a,b,c,1,2,3]
> 
> list=array[0]+ array[3]+ array[4]
> 
> list: [a,1,2]
> 
> When I do it like this: list=array[0]+ array[3]+ array[4] I get an
> error:
> 
> "TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and
> 'numpy.ndarray'"
> 
> Can you help me?

arr1 = [ 'a', 'b', 'c', 1, 2, 3 ]
# populate a new list with individual members
arr2 = [ arr1[0], arr1[3], arr1[5] ]
# create a new list by adding slices together
arr3 = arr1[:1] + arr1[2:4] + arr1[5:]
print arr2
# output is: ['a', 1, 3]
print arr3
# output is: ['a', 'c', 1, 3]

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web