问题描述:

In looking at other answers to this issue I found that the object was usually an integer so i constructed a simple example showing it is not and integer (or so I think), this code:

import numpy as np

a=np.arange(2,10)

print '1: ', a

print '2: ', a.size

print '3: ', a[3:] #this shows this is not an integer

print '3a: ', len(a[3:]) #len works

print '4: ', a.size[3:] #but yet size does not work

yields: ============

1: [2 3 4 5 6 7 8 9]

2: 8

3: [5 6 7 8 9]

4:

------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-12-5c4b06ceceba> in <module>()

4 print '2: ', a.size

5 print '3: ', a[3:] *#this shows this is not an integer*

----> 6 print '4: ', a.size[3:] *#but yet size does not work*

TypeError: 'int' object has no attribute '__getitem__'

======================

As you can see a[3:] is not an integer - what am I doing wrong?

网友答案:

If you want the size of a[3:] then try:

>>> a[3:].size
5

By writing a.size[3:] what you are trying to do is index over an integer as a.size is an integer.

相关阅读:
Top