Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (17.6k 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 (41.4k 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.

If you wish to know about Python visit this Python Course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.1k users

Browse Categories