Prepare > Python > Sets > Set .difference() Operation

2023. 7. 24. 20:28HackerRank-Python

 

Set .difference() Operation | HackerRank

Use the .difference() operator to check the differences between sets.

www.hackerrank.com

 

문제


Task

Students of District College have a subscription to English and French newspapers. Some students have subscribed to only the English newspaper, some have subscribed to only the French newspaper, and some have subscribed to both newspapers.

You are given two sets of student roll numbers. One set has subscribed to the English newspaper, and one set has subscribed to the French newspaper. Your task is to find the total number of students who have subscribed to only English newspapers.

 

Input Format

The first line contains the number of students who have subscribed to the English newspaper.
The second line contains the space separated list of student roll numbers who have subscribed to the English newspaper.
The third line contains the number of students who have subscribed to the French newspaper.
The fourth line contains the space separated list of student roll numbers who have subscribed to the French newspaper.

Output Format

Output the total number of students who are subscribed to the English newspaper only.

=> 영어신문만 구독하는 학생 수 구하라

 

 

 

 

코드


a = input()
b = set(input().split())
c = input()
d = set(input().split())

print(len(b - d))

 

 

 

 

노트


difference() 연산자

a.difference(b) # a 차집합 b. 아래와 결과 다름
b.difference(a) # b 차집합 a. 위와 결과 다름

a - b # 아래와 다름
b - a # 위와 다름
  • 연산자 (-)로도 사용 가능

 

 

두 번째 시도

a = int(input())
English = set(map(int, input().split()))

b = int(input())
French = set(map(int, input().split()))

print(len(English - French))