Your answer is one click away!

William February 2016
### Count object in Tab Separated Array in Python 2.7

I have an array of integer `r = [ 242 302 377 ..., 1090 225 203]`

. I would like to count the occurrences of `242`

in `r`

array. I used the count method like this:

```
asd = r.count(242)
print asd
```

but it gives me error

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

How to solve this?

fugu February 2016

Comma-separate your list values: `r = [ 242, 302, 242, 377, 1090, 225, 203]`

:

```
r = [ 242, 302, 242, 377, 1090, 225, 203]
asd = r.count(242)
print asd
```

Sharon February 2016

The easiest way to understand:

```
my_count = 0
for i in r:
if (i == 242):
my_count += 1
print my_count
```

E.Doroskevic February 2016

**Simple example:**

If you get to work with a list type of a structure supporting `.count()`

function you cann apply:

```
list.count(x)
```

where `list`

is a type of a listable collection, `count()`

is a function taking a single argument `x`

which identifies which element to check for occurrences

Else you could try and apply something like:

```
counter = 0
for x in list:
if x == 1:
counter += 1
print('Counter: ', counter)
```

where `list`

is a listable collection;

Dlucidone February 2016

Can try below code to compute -

Type 1-

```
r = [ 451, 242, 300, 424, 242, 567, 810, 242, 151, 413]
n= [i for i in r if i == 242]
print(len(n))
```

Type 2-

```
count = 0
r = [ 451, 242, 300, 424, 242, 567, 810, 242, 151, 413]
for i in r:
if i == 242:
count+=1
print(count)
```

hpaulj February 2016

There's no such thing as a 'tab separated array'. The display of `r`

is consistent with it being a `numpy array`

(as is the error message). It may have been loaded from a tab separated CSV. In any case, `count`

is a list method, not an array one. Either convert it to a list, or use one of the iterative solutions.

There is an array `bincount`

. Since your array appears to be integers in a reasonable range, e.g. 0-1000), it might apply here.

Make a sample array:

```
In [147]: r=np.random.randint(0,1000,2000)
In [148]: r
Out[148]: array([170, 754, 151, ..., 115, 299, 879])
```

Its `str`

display is:

```
In [166]: print(r)
[170 754 151 ..., 115 299 879]
```

This probably confuses Python programmers who don't know about `numpy`

.

`bincount`

finds the count for all values in the range:

```
In [152]: np.bincount(r)
Out[152]:
array([4, 1, 2, 1, 1, 5, 4, 3, 2, 1, 1, 1, 3, 3, 2, 4, 3, 2, 1, 1, 0, 1, 4,
...
1, 3, 0, 2, 1, 2, 3, 1, 2, 3, 3])
```

I probably should have used `np.bincount(r,minlength=1000)`

.

The 7th value in that count list is `4`

, so let's select that:

```
In [176]: np.bincount(r,minlength=1000)[6]
Out[176]: 4
```

I can use `count`

if I first convert `r`

to a list:

```
In [177]: r.tolist().count(6)
Out[177]: 4
```

The iterative solutions also work, but are slower:

```
def foo(a,v):
my_count=0
for i in r:
if (i==v):
my_count+=1
return my_count
In [178]: foo(r,6)
Out[178]: 4
```

time tests:

```
In [180]: timeit foo(r,6)
1000 loops, best of 3: 983 us per loop
In [181]: timeit len([i for i in r if i==6])
1000 loops, best of 3: 985 us per loop
In [182]: timeit r.tolist().count(6)
10000 loops, best of 3
```

```
```

```
```

```
```

```
```

```
```

```
```#### Post Status

Asked in February 2016

Viewed 1,833 times

Voted 13

Answered 5 times
#### Search

## Leave an answer

```
```

```
```

```
```# Quote of the day: live life