David Nunes - 6 months ago 27

Python Question

What question is asking for is a list that contains the number of all occurrences of the number 2 for a given index.

For example:

`[1, 2, 2]`

[2, 1, 1]

[1, 1, 2]

Gives

`[1,1,2]`

While

`[2, 2, 2]`

[2, 1, 2]

[2, 2, 1]

Gives:

`[3,2,2]`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

If I understand the problem correctly, you're trying to rotate the matrix by 90 degrees to the right, then count the number of twos in each of the rows in the matrix:

```
from numpy import array
l = [[1, 2, 2, 1, 1], [1, 1, 2, 2, 2], [1, 1, 1, 2, 2], [2, 2, 1, 2, 2], [2, 2, 1, 1, 1]]
result = [cur_l.count(2) for cur_l in array(l).T]
```

Output

```
print result
[2, 3, 2, 3, 3]
```

**Edit:**

Here's a solution without numpy:

```
>>> [cur_l.count(2) for cur_l in map(list, zip(*l))]
[2, 3, 2, 3, 3]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**