How to loop each array for Counter function inside a list using Python?

317 views Asked by At

I want to use counter function to count for the occurrences of the value 0 for each array inside a list.

from collections import Counter
[Counter(x) for x in a]
[Counter(x)[0] for x in a]

Using the above code it only applies to example like:

a = [array([-2, 0, 0]), array([-2, -1, 1])]

When it applies to the code below which have multiple arrays it raises a TypeError:

a = [[array([-2, 0, 0]), array([-2, -1, 1])], [array([3, -1]), array([1, -2])]]

Expected output:

[[2, 0], [0, 0]]

Can anyone help me?

1

There are 1 answers

0
poke On BEST ANSWER

Counter cannot magically descend your list and only count the elements in each array independently. It will always act upon the direct iterable you pass to it. In your first example, you iterate your list and then use each element to create a new Counter; since you have a flat list of array objects, you keep passing array objects to the call. In your second example however, you have a list of lists (which then contain array objects). So when you do the same thing as before, you try to create a counter from those lists. So what the counter tries to do is count how often a certain array object appears in that list. But as array objects are not hashable, it cannot identify them and you get that error. But that’s not the logic you want to use anyway.

Instead, you want to walk through all your lists and whenever you encounter an array, create a counter from it:

def convert (obj):
    if isinstance(obj, list):
        return list(map(convert, obj))
    else:
        return Counter(obj)[0]
>>> a = [[array([-2, 0, 0]), array([-2, -1, 1])], [array([3, -1]), array([1, -2])]]
>>> convert(a)
[[2, 0], [0, 0]]
>>> b = [array([-2, 0, 0]), array([-2, -1, 1])]
>>> convert(b)
[2, 0]