Python | Test list element similarity
Given a list, your task is to determine the list is K percent same i.e has element that is populated more than K % times.
Given below are few methods to solve the task.
Method #1 Using collections.Counter
Python3
from collections import Counter
ini_list1 = [ 1 , 2 , 3 , 1 , 1 , 1 , 1 , 1 , 3 , 2 ]
print ("Initial list ", ini_list1)
K = 60
i, freq = Counter(ini_list1).most_common( 1 )[ 0 ]
if len (ini_list1) * (K / 100 ) < = freq:
print (" True ")
else :
print (" False ")
|
Output:
Initial list [1, 2, 3, 1, 1, 1, 1, 1, 3, 2]
True
Method #2: Using dictionary and its values
Python3
from collections import Counter, defaultdict
ini_list1 = [ 1 , 2 , 3 , 1 , 1 , 1 , 1 , 1 , 3 , 2 ]
print ("Initial list ", ini_list1)
K = 60
freq = defaultdict( int )
for x in ini_list1:
freq[x] + = 1
freq = freq.values()
if max (freq) > = (K / 100 ) * sum (freq):
print (" True ")
else :
print (" False ")
|
Output:
initial list [1, 2, 3, 1, 1, 1, 1, 1, 1, 1]
True
Method #3: Using List comprehension and set()
Python3
ini_list1 = [ 1 , 2 , 3 , 1 , 1 , 1 , 1 , 1 , 3 , 2 ]
print ( "Initial list" , ini_list1)
K = 60
count = len ([x for x in set (ini_list1) if ini_list1.count(x) > = (K / 100 ) * len (ini_list1)])
if count > 0 :
print ( "True" )
else :
print ( "False" )
|
Output
Initial list [1, 2, 3, 1, 1, 1, 1, 1, 3, 2]
True
In method #3, we use list comprehension and set() to check if the given list is K percent same i.e has element that is populated more than K % times.
First, the program creates a set of all the unique elements in the list, then using list comprehension, it counts the number of occurrences of each unique element in the original list. The program then checks if the count of any of the unique elements is greater than or equal to (K/100) * len(ini_list1), this check is performed for all the unique elements in the list. If the count of any of the elements is greater than the required value, the program returns True, else it returns False.
Method #4: Using a for loop:
Python3
def is_k_percent_same(lst, k):
count = {}
for num in lst:
if num in count:
count[num] + = 1
else :
count[num] = 1
most_common = max (count.values())
if most_common > = len (lst) * (k / 100 ):
return True
else :
return False
ini_list1 = [ 1 , 2 , 3 , 1 , 1 , 1 , 1 , 1 , 3 , 2 ]
print ( "Initial list" , ini_list1)
K = 60
print (is_k_percent_same(ini_list1, K))
|
Output
Initial list [1, 2, 3, 1, 1, 1, 1, 1, 3, 2]
True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5: Using numpy.bincount()
- Import the numpy module.
- Pass the list ‘lst’ to numpy.bincount() to obtain the counts of each element in ‘lst’.
- Get the maximum count from the bincount array.
- Calculate the threshold value (len(lst) * (k/100)).
- Check if the maximum count is greater than or equal to the threshold value.
- Return True if the condition is satisfied, False otherwise.
Python3
import numpy as np
def is_k_percent_same(lst, k):
bincount = np.bincount(lst)
most_common = np. max (bincount)
threshold = len (lst) * (k / 100 )
if most_common > = threshold:
return True
else :
return False
ini_list1 = [ 1 , 2 , 3 , 1 , 1 , 1 , 1 , 1 , 3 , 2 ]
print ( "Initial list" , ini_list1)
K = 60
print (is_k_percent_same(ini_list1, K))
|
OUTPUT :
Initial list [1, 2, 3, 1, 1, 1, 1, 1, 3, 2]
True
Time Complexity: O(n)
Auxiliary Space: O(max(lst)) (for creating the bincount array)
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...