Chrinide Wu
Chrinide Wu

Reputation: 119

Count the same list's occur frequency in a multi-dimensional list?

I have a multi-dimensional list as like below

multilist = [[1,2],[3,4,5],[3,4,5],[5,6],[5,6],[5,6]]

How can I get below results fast:

[1,2]: count 1 times
[3,4,5]: count 2 times
[5,6]: count 3 times

and also get the unique multi-dimensional list (remove duplicates) :

multi_list = [[1,2],[3,4,5],[5,6]]

Thanks a lot.

Upvotes: 0

Views: 417

Answers (6)

hspandher
hspandher

Reputation: 16733

You can use a dictionary for this

count_data = {}

for my_list in multilist:
    count_data.setdefault(tuple(my_list), 0)
    count_data[tuple(my_list)] += 1

Upvotes: -1

nigel222
nigel222

Reputation: 8192

How about using repr( alist) to convert it to its text string representation?

from collections import defaultdict

d = defaultdict(int)
for e in multilist: d[ repr(e)] += 1

for k,v in d.items(): print "{0}: count {1} times".format( k,v)

Upvotes: 0

Adem Öztaş
Adem Öztaş

Reputation: 21446


You can try like this,

>>> multilist = [[1,2],[3,4,5],[3,4,5],[5,6],[5,6],[5,6]]
>>> c = [multilist.count(l) for l in multilist]
>>> for ind, l in enumerate(multilist):
...   print( "%s: count %d times" % (str(l), c[ind]))
... 
[1, 2]: count 1 times
[3, 4, 5]: count 2 times
[3, 4, 5]: count 2 times
[5, 6]: count 3 times
[5, 6]: count 3 times
[5, 6]: count 3 times

>>> {str(item): multilist.count(item) for item in multilist }
{'[1, 2]': 1, '[3, 4, 5]': 2, '[5, 6]': 3}

Upvotes: 0

thebjorn
thebjorn

Reputation: 27311

If you want to guarantee that the order of the unique items is the same as in the original list, you could do something like:

>>> class Seen(set):
...     def __contains__(self, item):
...         res = super(Seen, self).__contains__(item)
...         self.add(item)
...         return res
...
>>> seen = Seen()
>>> [item for item in multilist if tuple(item) not in seen]
[[1, 2], [3, 4, 5], [5, 6]]
>>>

Upvotes: 1

Reut Sharabani
Reut Sharabani

Reputation: 31339

You can use tuples which are hashable and collections.Counter:

>>> multilist = [[1,2],[3,4,5],[3,4,5],[5,6],[5,6],[5,6]]
>>> multituples = [tuple(l) for l in multilist]
>>> from collections import Counter
>>> tc = Counter(multituples)
>>> tc
Counter({(5, 6): 3, (3, 4, 5): 2, (1, 2): 1})

To get the set of elements you just need the keys:

>>> tc.keys()
dict_keys([(1, 2), (3, 4, 5), (5, 6)])

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

As @ReutSharabani suggested, you can use tuples as dictionary keys, and then convert back to lists for display purposes. The code below doesn't reply on collections (not that there's anything wrong with that).

multilist = [[1,2],[3,4,5],[3,4,5],[5,6],[5,6],[5,6]]
histogram = {}
for x in multilist:
    xt = tuple(x)
    if xt not in histogram:
        histogram[xt] = 1
    else:
        histogram[xt] += 1
for k,c in histogram.items():
    print "%r: count %d times" % (list(k),c)
print [list(x) for x in histogram.keys()]

Upvotes: 0

Related Questions