set

hyyyynjn·2021년 10월 29일
0

python 정리

목록 보기
19/26
post-thumbnail

부분집합 확인

  • <, <= 부등호로 부분집합 확인

https://stackoverflow.com/questions/16579085/how-can-i-verify-if-one-list-is-a-subset-of-another

>>> a = [1, 3, 5]
>>> b = [1, 3, 5, 8]
>>> c = [3, 5, 9]
>>> set(a) <= set(b)
True
>>> set(c) <= set(b)
False

>>> a = ['yes', 'no', 'hmm']
>>> b = ['yes', 'no', 'hmm', 'well']
>>> c = ['sorry', 'no', 'hmm']
>>> 
>>> set(a) <= set(b)
True
>>> set(c) <= set(b)
False
  • issubset() 메소드로 부분집합 확인

https://www.programiz.com/python-programming/methods/set/issubset

A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5}

# Returns True
print(A.issubset(B))

# Returns False
# B is not subset of A
print(B.issubset(A))

# Returns False
print(A.issubset(C))

# Returns True
print(C.issubset(B))

0개의 댓글