0 votes
1 view
in Data Science by (13.1k points)

Starting with data formatted like this:

data = [[0,1],[2,3],[0,1],[0,2]]

I would like to represent every value once with its frequency:

output = [[[0,1],2],[[2,3],1],[[0,2],1]]

I've found many solutions to this problem for 1D lists, but they don't seem to work for 2D.

1 Answer

0 votes
by (19.9k points)

Refer to the code below where we are using collections.Counter():

>>> from collections import Counter

>>> 

>>> Counter(map(tuple,data))

Counter({(0, 1): 2, (2, 3): 1, (0, 2): 1})

>>> Counter(map(tuple,data)).items()

[((0, 1), 2), ((2, 3), 1), ((0, 2), 1)]

You need to convert the list objects to tuple which is a hashable object because you cannot use them as dictionary keys as list objects are not hashable.

...