Lists and arrays

A

Ana Dionísio

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?
 
D

Dave Angel

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] ]
 
B

BartC

Ana Dionísio said:
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.
 
8

88888 Dihedral

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 alist, 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.
 
D

Denis McMahon

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top