Python | Find most common element in each column in a 2D list
Last Updated :
17 Apr, 2023
Given a 2D list, write a Python program to find the most common element in each column of the 2D list. Examples:
Input : [[1, 1, 3],
[2, 3, 3],
[3, 2, 2],
[2, 1, 3]]
Output : [2, 1, 3]
Input : [['y', 'y'],
['y', 'x'],
['x', 'x']]
Output : ['y', 'x']
Method #1 : Using most_common() from collections module most_common() return a list of the n most common elements and their counts from the most common to the least. Thus, we can easily find the most common elements in each column using list comprehension.
Python3
from collections import Counter
def mostCommon(lst):
return [Counter(col).most_common( 1 )[ 0 ][ 0 ] for col in zip ( * lst)]
lst = [[ 1 , 1 , 3 ],
[ 2 , 3 , 3 ],
[ 3 , 2 , 2 ],
[ 2 , 1 , 3 ]]
print (mostCommon(lst))
|
Time Complexity: O(n*n), where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list
Method #2 : Using mode() from statistics module
Python3
from scipy.stats import mode
import numpy as np
def mostCommon(lst):
val, count = mode(lst, axis = 0 )
return val.ravel().tolist()
lst = [[ 1 , 1 , 3 ],
[ 2 , 3 , 3 ],
[ 3 , 2 , 2 ],
[ 2 , 1 , 3 ]]
print (mostCommon(lst))
|
Method #3 : Using recursion
This code defines a function most_common that takes a list of lists (lst) and an optional argument col (which is 0 by default). The function returns a list containing the most common element in each column of the input list.
The function first checks if col is equal to the length of the first sublist in lst. If it is, the function returns an empty list. This check is included so that the function knows when to stop iterating through the columns of lst.
Next, the function initializes an empty dictionary called count. This dictionary will be used to count the frequency of each element in the current column of lst.
The function then iterates through each row in lst and increments the count for the element in the col-th position of that row in the count dictionary. If the element is not already in the dictionary, it is added with a count of 1.
After all the rows have been processed, the function returns a new list with the element that appears most frequently in the col-th position of lst (which is found using the max function and the count.get key function) as the first element, and the result of calling most_common on lst with col incremented by 1 as the rest of the elements.
Finally, the code calls the most_common function on the list lst and prints the result.
Python
def most_common(lst, col = 0 ):
if col = = len (lst[ 0 ]):
return []
count = {}
for row in lst:
if row[col] in count:
count[row[col]] + = 1
else :
count[row[col]] = 1
result = [ max (count, key = count.get)] + most_common(lst, col + 1 )
return result
lst = [[ 1 , 1 , 3 ], [ 2 , 3 , 3 ], [ 3 , 2 , 2 ], [ 2 , 1 , 3 ]]
print (most_common(lst))
|
Time complexity: O(mn), where m is the number of rows and n is the number of columns in the list.
Auxiliary Space: O(n), where n is the number of columns in the list.
Method 4: Using nested loops and dictionaries.
Algorithm:
- Initialize an empty list res to store the most common element in each column.
- Iterate over each column j in the 2D list lst.
- Initialize an empty dictionary count_dict to count the occurrence of each unique element in the column.
- Iterate over each row i in the column j of the 2D list lst.
- If the element lst[i][j] is already in the count_dict, increment its count by 1. Otherwise, add it to the dictionary with a count of 1.
- Find the element with the maximum count in the count_dict using the max() function and the key argument to specify the sorting criteria.
- Append the most common element to the res list.
- Return the res list.
Python3
lst = [[ 1 , 1 , 3 ],
[ 2 , 3 , 3 ],
[ 3 , 2 , 2 ],
[ 2 , 1 , 3 ]]
res = []
for j in range ( len (lst[ 0 ])):
count_dict = {}
for i in range ( len (lst)):
if lst[i][j] in count_dict:
count_dict[lst[i][j]] + = 1
else :
count_dict[lst[i][j]] = 1
res.append( max (count_dict, key = count_dict.get))
print (res)
|
Let n be the number of rows and m be the number of columns in the input 2D list lst.
Time Complexity: The outer loop iterates m times, and the inner loop iterates n times, so the overall time complexity is O(n * m).
Space Complexity: The space used by the res list is O(m), and the space used by the count_dict dictionary is at most O(n), so the overall space complexity is O(max(n, m)).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...