YD Han YD Han - 3 months ago 7
Python Question

only one element in numpy array shape

I have an array converted from list, when I try to get its shape, I got only one number. like this:

list1=[1,2,3,4,5]
a1=numpy.array(list1)
print a1.shape


and I got

(5,)


and then I tried

list2=[[1,2,3,4,5]]
a2=numpy.array(list2)

list3=[[1],[2],[3],[4],[5]]
a3=numpy.array(list3)

print a1+a2
print a1+a3


I get

[[ 2 4 6 8 10]]

[[ 2 3 4 5 6]
[ 3 4 5 6 7]
[ 4 5 6 7 8]
[ 5 6 7 8 9]
[ 6 7 8 9 10]]


it seems a1 works like a2. Can I think like that way? Will it cause problems if i treat a1 as a2, besides shape method?

Answer

Try:

list1=[[1,2,3,4,5]]
a=numpy.array(list1)
print a.shape

This will give you (1, 5), one row, 5 columns.

And this:

list1=[[1],[2],[3],[4],[5]]
a=numpy.array(list1)
print a.shape

Will get you (5, 1)