Sometimes, while working with python Matrix, we can have a problem in which we need to find frequencies of all elements in Matrix. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using Counter() + sum() + map()
The combination of above methods can be used to perform this task. In this, we perform task of counting elements using Counter() and extending the logic to each row is performed using sum() and map().
# Python3 code to demonstrate # Matrix elements Frequencies Counter # using Counter() + sum() + map() from collections import Counter # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print("The original list is : " + str(test_list)) # Matrix elements Frequencies Counter # using Counter() + sum() + map() res = dict(sum(map(Counter, test_list), Counter())) # printing result print ("The frequencies dictionary is : " + str(res)) |
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : {2: 1, 4: 2, 5: 3, 6: 2, 7: 1}
Method #2 : Using chain() + Counter()
The combination of above functions can be used to perform this task. In this, we flatten the matrix using chain() and compute frequency using Counter().
# Python3 code to demonstrate # Matrix elements Frequencies Counter # using Counter() + chain() from collections import Counter import itertools # Initializing list test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]] # printing original list print("The original list is : " + str(test_list)) # Matrix elements Frequencies Counter # using Counter() + chain() res = dict(Counter(itertools.chain(*test_list))) # printing result print ("The frequencies dictionary is : " + str(res)) |
The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : {2: 1, 4: 2, 5: 3, 6: 2, 7: 1}
Recommended Posts:
- Python - Counter.items(), Counter.keys() and Counter.values()
- Python dictionary, set and counter to check if frequencies can become same
- Python Counter| Find duplicate rows in a binary matrix
- Python | Minimum number of subsets with distinct elements using Counter
- Python | Counter Objects | elements()
- Anagram checking in Python using collections.Counter()
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Dictionary and counter in Python to find winner of election
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python Counter to find the size of largest subset of anagram words
- Python Counter | Majority Element
- Operations on Python Counter
- Python | Case Counter in String
- Python - Occurrence counter in List of Records
- Python - Case Insensitive string counter
- Count frequencies of all elements in array in Python using collections module
- Python - Consecutive elements maximum frequencies
- Counting the frequencies in a list using dictionary in Python
- Python - Associated Values Frequencies in Dictionary
- Python - Frequencies of Values in a Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

