0 Comments

I have a list L containing sublists as follows:

L = [[‘16.37.123.153’, ‘119.222.456.130’, ‘38673’, ‘161’, ’17’, ’62’, ‘4646’], [‘16.37.456.153’, ‘119.222.123.112’, ‘56388’, ‘161’, ’17’, ’62’, ‘4646’], …]

I want to find the 5 most occurring sublists from L using collections.Counter. However, when I try the solution below:

most_frequent_elements, counter_of_elements = zip(*Counter(L).most_common(5))

I encounter the following error:

Traceback (most recent call last): File “hypergraph.py”, line 65, in <module> most_frequent, counter_mfi = zip(*Counter(L).most_common(5)) File “/usr/lib/python2.7/collections.py”, line 477, in __init__ self.update(*args, **kwds) File “/usr/lib/python2.7/collections.py”, line 567, in update self[elem] = self_get(elem, 0) + 1 TypeError: unhashable type: ‘list’

How can I modify the solution to handle my type of list? I’m looking for an optimized solution with efficient time complexity.

Example:

L = [[‘16.37.123.153’, ‘119.222.456.130’, ‘38673’, ‘161’, ’17’, ’62’, ‘4646’], [‘16.37.456.153’, ‘119.222.123.112’, ‘56388’, ‘161’, ’17’, ’62’, ‘4646’], …]
#Output: Most frequent elements: List of the 5 most occurring sublists Counter: List of occurrence counts for the 5 sublists”

Anish Asked question May 29, 2023