Write A Python Function That Takes Two Lists And Returns The Number Of Common Members

Given a two list of integers As an input  argument to the Python function,  the Python function should find the matching elements between two lists of integers and then print the The Count of number of matching integers.


For example let's take a below list

l1 = [1, 2, 3, 4, 5, 5]
l2 = [4, 4, 5, 6, 7, 8]

The matching elements are four and five.  so the count is two.

 

Write A Python Function That Takes Two Lists And Returns The Number Of Common Members

 




Write A Python Function That Takes Two Lists And Returns The Number Of Common Members


Let's take a function name as count_common()  and pass that to the list as an input to the Python function,  take a variable number and set it to zero Sunday cell count the number of common numbers between two  lists.


iterate all the items present in the list by converting it to a set and check if an item is present in the list to if so increment the number and if the following is completed return the number.


def count_common(list1, list2):
   number = 0
   for item in set(list1):
       if item in list2:
           number += 1
   return number




In the main program, take a list and Take a variable to store the return value after calling the count_common().  once the function is called  print the the variable common


l1 = [1, 2, 3, 4, 5, 5]
l2 = [4, 4, 5, 6, 7, 8]
common = count_common(l1, l2)
print("Number of Common Members are : ", common)



Final program
==================

def count_common(list1, list2):
   number = 0
   for item in set(list1):
       if item in list2:
           number += 1
   return number


l1 = [1, 2, 3, 4, 5, 5]
l2 = [4, 4, 5, 6, 7, 8]
common = count_common(l1, l2)
print("Number of Common Members are : ", common)






Output
================

Number of Common Members are :  2



The input has below list the output will be  as shown below.

Input
==============

l1 = [1, 2, 3, 4, 5, 5]
l2 = [4, 4, 6, 7, 8, 9]



OUTPUT
===============

Number of Common Members are :  1




Other Version to get input from the user
==============================================

In the below version of the program we are asking users to enter the list1 length and then enter all the list1 items.  Similarly we ask users to enter the list1 length  and list2 items. Then called  function to count the matching numbers between two list.



def count_common(list1, list2):
   number = 0
   for item in set(list1):
       if item in list2:
           number += 1
   return number


n1 = int(input("Enter List1 Length : "))
l1 = []
for _ in range(n1):
   l1.append(int(input("Enter List1 Items : ")))

l2 = []
n2 = int(input("Enter List2 Length : "))
for _ in range(n2):
   l2.append(int(input("Enter List1 Items : ")))

common = count_common(l1, l2)
print("Number of Common Members are : ", common)



OUTPUT
==============

Enter List1 Length : 3
Enter List1 Items : 1
Enter List1 Items : 2
Enter List1 Items : 3
Enter List2 Length : 4
Enter List1 Items : 1
Enter List1 Items : 3
Enter List1 Items : 4
Enter List1 Items : 5
Number of Common Members are :  2




Conclusion
=====================

Execute the above program by providing the random integers to both the list and then more than the result.  Comment down below  if you have any suggestions regarding the above program.




Post a Comment

0 Comments