예시)
old = [1, 2, 3, 4, 5, 1, 2, 3]
new = set(old)
print(new)
출력)
{1, 2, 3, 4, 5}
set(), {}
를 이용해서 만들어진다 .add
, .remove()
로 요소 추가, 삭제 가능하다.set()
안에 저장되어 있는 값을 변경할 수 없다.intersection
, union
함수로 교집합과 합집합을 구할 수 있다set()
처럼 순서는 없지만 indexing이 가능하다. 값 수정이 가능하며 {}
를 이용한다.key - value
를 같이 저장하는 data type
이다. dictionary
안에 있는 모든 key
는 유니크(고유)하다key
를 입력시 기존 key-value
pair는 새로운 key
replace 된다.예시)
coding = {'hard' : 'worth it'}
coding['hard'] = 'totally!'
print(coding)
출력) 기존 value
가 다른 value
로 교체 된 것을 볼 수 있다
{'hard': 'totally!'}
dictionary
또한 추가 삭제가 가능하다. 추가 예시)
coding = {'hard' : 'worth it'}
coding['difficult'] = 'challenging'
print(coding)
출력)
{'hard': 'worth it', 'difficult': 'challenging'}
삭제 예시)
coding = {'hard': 'worth it', 'difficult': 'challenging','easy' : 'genius'}
del coding['hard']
print(coding)
출력)
{'difficult': 'challenging', 'easy': 'genius'}
keys()
, values()
, items()
, clear()
, get(x)
, x in dic
를 이용해서 다양한 출력과 활용을 할 수 있다. # 제 키를 수정해 보겠습니다
minji = [158, '백오십팔']
minji[0] = 157.5
minji[1] = '반올림금지'
print(minji)
출력)
# 다들 반올림 하면서...
[157.5, '반올림금지']
[]
을 사용한다.TypeError: 'tuple' object does not support item assignment
가 나올 것이다.()
을 사용한다. 하지만 소괄호 없이 값 사이에 ,
만 써도 튜플로 인식한다.예시)
tuple = (1, 2, 'blue')
new_tuple = 1, 2, 'blue'
Reference:
https://dev.to/arvindmehairjan/what-are-the-differences-between-a-list-tuple-dictionary-set-in-python-2lm6
https://m.blog.naver.com/PostView.nhn?blogId=pgh7092&logNo=221103514790&proxyReferer=https:%2F%2Fwww.google.com%2F