문제보기
Problem
Objective
Today, we're learning about a new data type: sets.
Concept
If the inputs are given on one line separated by a space character, use split() to get the separate values in the form of a list:
>> a = raw_input()
5 4 3 2
>> lis = a.split()
>> print (lis) ['5', '4', '3', '2']
If the list values are all integer types, use the map() method to convert all the strings to integers.
>> newlis = list(map(int, lis))
>> print (newlis)
[5, 4, 3, 2]
Sets are an unordered bag of unique values. A single set contains values of any immutable data type.
CREATING SETS
>> myset = {1, 2} # Directly assigning values to a set
>> myset = set() # Initializing a set
>> myset = set(['a', 'b']) # Creating a set from a list
>> myset {'a', 'b'}
MODIFYING SETS
Using the add() function:
>> myset.add('c')
>> myset {'a', 'c', 'b'}
>> myset.add('a') # As 'a' already exists in the set, nothing happens
>> myset.add((5, 4))
>> myset {'a', 'c', 'b', (5, 4)}
Using the update() function:
>> myset.update([1, 2, 3, 4]) # update() only works for iterable objects
>> myset
{'a', 1, 'c', 'b', 4, 2, (5, 4), 3}
>> myset.update({1, 7, 8})
>> myset
{'a', 1, 'c', 'b', 4, 7, 8, 2, (5, 4), 3}
>> myset.update({1, 6}, [5, 13])
>> myset
{'a', 1, 'c', 'b', 4, 5, 6, 7, 8, 2, (5, 4), 13, 3}
REMOVING ITEMS
Both the discard() and remove() functions take a single value as an argument and removes that value from the set. If that value is not present, discard() does nothing, but remove() will raise a KeyError exception.
>> myset.discard(10)
>> myset
{'a', 1, 'c', 'b', 4, 5, 7, 8, 2, 12, (5, 4), 13, 11, 3}
>> myset.remove(13)
>> myset
{'a', 1, 'c', 'b', 4, 5, 7, 8, 2, 12, (5, 4), 11, 3}
COMMON SET OPERATIONS
Using union(), intersection() and difference() functions.
>> a = {2, 4, 5, 9}
>> b = {2, 4, 11, 12}
>> a.union(b) # Values which exist in a or b
{2, 4, 5, 9, 11, 12}
>> a.intersection(b) # Values which exist in a and b
{2, 4}
>> a.difference(b) # Values which exist in a but not in b
{9, 5}
The union() and intersection() functions are symmetric methods:
>> a.union(b) == b.union(a)
True
>> a.intersection(b) == b.intersection(a)
True
>> a.difference(b) == b.difference(a)
False
These other built-in data structures in Python are also useful.
문제부터 이해가 힘들었음....
M,arr=input(),set(map(int,input().split()))
N,arr1=input(),set(map(int,input().split()))
x=(arr.union(arr1)).difference((arr.intersection(arr1)))
print(*sorted(x,key=int),sep="\n")
m = int(input())
m_set = set(map(int, input().split()))
n = int(input())
n_set = set(map(int, input().split()))
print('\n'.join(map(str, sorted(m_set ^ n_set))))
if __name__ == '__main__' :
ans_ = set()
M = int(input())
set_m_ = set(list(map(int, input().split()))[:M])
N = int(input())
set_n_ = set(list(map(int, input().split()))[:N])
ans_.update(set_m_.difference(set_n_))
ans_.update(set_n_.difference(set_m_))
for i in sorted(ans_):
print(i)
'IT & 영상관련 > 파이썬python' 카테고리의 다른 글
HackerRank] introducation to sets 셋 (저장용) (0) | 2020.08.08 |
---|---|
HackerRank] collections.Counter() (저장용) (0) | 2020.08.07 |
HackerRank] .combinations() 조합 (저장용) (2) | 2020.08.06 |
HackerRank] .permutations() 순열 (저장용) (0) | 2020.08.05 |
HackerRank] itertools.product() 리스트 간 곱하기 (저장용) (0) | 2020.08.04 |