124k views
4 votes
Programming Objectives

With two given lists, such as [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection (common) of the above given lists. Please note that redundant elements should be counted only once.
Technical Descriptions
Your program should interact with its user such that two lists of different number of elements can be arbitrarily specified through program input.
With the lists above as an example:
Please specify the number of elements in the first list:
6
Please specify the number of elements in the second list:
7
Input element 1 of list 1:
1
Input element 2 of list 1:
3
Input element 3 of list 1:
6
Input element 4 of list 1:
78
Input element 5 of list 1:
35
Input element 6 of list 1:
55
Input element 1 of list 2:
12
Input element 2 of list 2:
24
Input element 3 of list 2:
35
Input element 4 of list 2:
24
Input element 5 of list 2:
88
Input element 6 of list 2:
120
Input element 7 of list 2:
155
The expected output should be
Language: Python

1 Answer

4 votes

Answer:

See Explaination

Step-by-step explanation:

m=int(input("Please specify the number of elements in first list : "))

n=int(input("Please specify the number of elements in second list : "))

m1=1

list1=[]

n1=1

list2=[]

while m1<=m:

r1=int(input("input element {} of list1: ".format(m1)))

list1.append(r1)

m1+=1

while n1<=n:

list2.append(int(input("input element {} of list2: ".format(n1))))

n1+=1

def Intersection(list1,list2):

return set(list1).intersection(list2)

print("The intersection of two lists is {}".format(Intersection(list1,list2)))

User VAr
by
5.4k points