unique element intersection python

Solutions on MaxInterview for unique element intersection python by the best coders in the world

showing results for - "unique element intersection python"
Maite
04 Jul 2016
1#get name1 and name2 as a list input
2name1 =list("".join(str(x)for x in input("Enter name1").replace(" ","")))
3name2 =list("".join(str(x)for x in input("Enter name2").replace(" ","")))
4#check using list comprehension if x in name1 is in name2
5#this will return multiple instances of the same character from name1 that matches with the name2
6common = [x for x in name1 if x in name2]
7#create a set out of the output so as to have only unique values of the repeated characters
8unique = set(common)
9#thus the above set will have common unrepeated characters from both names
10#create a variable and initialize it to zero
11d=0
12#run a loop that checks the minimum occurrence of 
13#the character from the set in name1 & name2
14#Minimum because for ex: a might exist thrice in name1, but only twice in name2
15#we will need to take only 2 common occurrences from the name2
16#thus finding the minimum occurrence of the character from both names
17for x in unique:
18    d = d + min(name1.count(x),name2.count(x))
19#multiplying by two, because if one character from name 1 matches with one,
20#character from name2, then it makes two in total
21difference = (len(name1) + len(name2)) - d*2
22print(difference)