Python – Test if common values are greater than K

Sometimes, while working with Lists, we can have a problem in which we need to track common elements in list. This is quite common and we often have constructs to solve this. But sometimes, we require to consider two list as matching if they contain aleast K matching elements. Lets discuss certain ways in which this can be performed.

Method #1 : Using set() + len()
The combination of above methods can be used to solve this task. In this we first convert each list into set and then use len() to check if matching elements are greater than K.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Test if common values are greater than K
# using len() + set()
  
# Initializing lists
test_list1 = ['Gfg', 'is', 'for', 'Geeks']
test_list2 = [1, 'Gfg', 2, 'Geeks']
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Initializing K 
K = 2
  
# Test if common values are greater than K
# using len() + set()
res = len(set(test_list1) & set(test_list2)) >= K
  
# printing result 
print ("Are common elements greater than K ? : " + str(res))

chevron_right


Output :

The original list 1 is : ['Gfg', 'is', 'for', 'Geeks']
The original list 2 is : [1, 'Gfg', 2, 'Geeks']
Are common elements greater than K ? : True

 

Method #2 : Using sum()
This is yet another way in which this problem can be solved. In this, we just take count of all the column elements and sum them using sum() and then check with K.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Test if common values are greater than K
# using sum()
  
# Initializing lists
test_list1 = ['Gfg', 'is', 'for', 'Geeks']
test_list2 = [1, 'Gfg', 2, 'Geeks']
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Initializing K 
K = 2
  
# Test if common values are greater than K
# using sum()
res = sum(i in test_list1 for i in test_list2) >= 2
  
# printing result 
print ("Are common elements greater than K ? : " + str(res))

chevron_right


Output :

The original list 1 is : ['Gfg', 'is', 'for', 'Geeks']
The original list 2 is : [1, 'Gfg', 2, 'Geeks']
Are common elements greater than K ? : True

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.