-1

array1=[ 0 5 6 6 6 0 6 0 6 8 0 19 24 7 0 4 9 14 12 0 22 17 1 0 19 6 17 4 7 0 17 24 0 6 9 22]

i=0
while i<23
      m= array1.count(i)
      i=i+1

AttributeError: 'numpy.ndarray' object has no attribute 'count'

Why does attribute error appear when i use .count()? do I need to import something?

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
PythonAlex
  • 241
  • 2
  • 4
  • 8
  • 1
    Are you sure your array is formated correctly? array1 = [0, 5, 6, ...] – veiset May 10 '11 at 11:30
  • The array is precisely how it appears :s – PythonAlex May 10 '11 at 11:33
  • It seems like you are trying to use numpy.ndarry. `array1 = [0, 5, 6, 6,...]` followed by `for i in range(0,23): print array1.count(i)` will do the trick. Are you using the ndarray intentionally? :) – veiset May 10 '11 at 11:38

4 Answers4

8

Well, according to the documentation, ndarray simply has no count method.

The code you have posted contradicts the error-message you give us. In your code you create a simple Python list, but your error message indicates that you are actually using a numpy ndarray.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

What you try to do, can be done much more efficiently (Python 2.7 and above) by:

import numpy as np
from collections import Counter
array1= np.array([ 0,  5,  6,  6,  6,  0,  6,  0,  6,  8,  0, 19, 24,  7,  0,  4,  9, 14, 12,  0, 22, 17,  1,  0, 19, 6, 17,  4,  7,  0, 17, 24,  0,  6,  9, 22])
print Counter(array1.most_common(1))
ntg
  • 12,950
  • 7
  • 74
  • 95
0

First off all, your array is formatted weirdly, there should be commas between the numbers. Second, you are creating a numpy.ndarray from the numpy package, not a native python list. Use a python list, and it should work.

Oskar
  • 889
  • 7
  • 20
0

try to break line with "\"
and add "," between numbers.
How can I do a line break (line continuation) in Python?

Community
  • 1
  • 1
shevski
  • 1,012
  • 1
  • 10
  • 32
  • Well the array is actually inputed in text but converted into numbers... So I'm not quite sure how i'd add , between the numbers – PythonAlex May 10 '11 at 11:42
  • 1
    Oh, wait, so that first line isn't actually in the code, it's converted from text? Well, the error that's in the code has to do with how you set up array1, you're creating it as a numpy array, not a python list, and numpy arrays doesn't have a count function. Are you really sure you want it to be a numpy-array? It would be helpful to see the code that sets up the variable. – Oskar May 10 '11 at 11:45
  • @PythonAlex : that should be made when the array is defined, if you get the array defined that something else – shevski May 10 '11 at 11:46