

[출처]https://cheris8.github.io/python/DS-Overview/
| 메서드 | 설명 |
|---|---|
| s.add(x) | 세트 s에 항목 x를 추가. 이미 x가 있다면 변화 없음 |
| s.clear() | 세트 s의 모든 항목을 제거 |
| s.remove(x) | 세트 s에서 항목 x를 제거. 항목 x가 없을 경우 key error |
| s.pop() | 세트 s에서 랜덤하게 항목을 반환하고, 해당 항목을 제거 |
| s.discard(x) | 세트 s에서 항목 x를 제거 |
| s.update(iterable) | 세트 s에 다른 iterable 요소를 추가 |
s.add(x)
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.add(d)
print(my_set) #{1, 'b', 3, 2, 'c', 'd', 'a'}
my_set.add(d)
print(my_set) #{1, 'b', 3, 2, 'c', 'd', 'a'}
s.clear()
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.clear()
print(my_set)
s.remove(x)
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.remove(2)
print(my_set) # {'b', 1, 3, 'c', 'a'}
my_set.remove(10)
print(my_set) # key error
s.dicard()
my_set = {1, 2, 3}
my_set.discard(2)
print(my_set) # {1, 3, 'a', 'c', 'b'}
my_set.discard(10)
s.pop()
my_set = {'a', 'b', 'c', 1, 2, 3}
element = my_set.pop()
print(element) # 1
print(my_set) # {2, 3, 'b', 'a', 'c'}
s.update()
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.update([1, 4, 5])
print(my_set) # {1, 2, 3, 'c', 4, 5, 'b', 'a' }
| 메서드 | 설명 | 연산자 |
|---|---|---|
| set1.difference(set2) | set1에는들어 있지만 set2에는 없는 항목으로 세트를 생성후 반환 | set1 - set2 |
| set1.intersection(set2) | set1과 set2 모두 들어 있는 항목으로 세트를 생성후 반환 | set1 & set2 |
| set1.issubset(set2) | set1의 항목이 모두 set2에 들어있으면 True를 반환 | set1 <= set2 |
| set1.issuperset(set2) | set1 가 set2의 항목을 모두 포함하면 True를 반환 | set1 >= set2 |
| set1.union(set2) | set1 또는 set2에 들어있는 항목으로 세트를 생성 후 반환 | set1\set2 |
set1 = {0, 1, 2, 3, 4}
set2 = {1, 3, 5, 7, 9}
print(set1.difference(set2)) # {0, 2, 4}
print(set1.intersection(set2)) # {1, 3}
print(set1.issubset(set2)) # False
print(set1.issuperset(set2)) # False
print(set1.union(set2)) # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
| 메서드 | 설명 |
|---|---|
| D.clear() | 딕셔너리 D의 코든 키/값 쌍을 제거 |
| D.get(k) | 키 k에 연결된 값을 반환 (키가 없으면 None을 반환) |
| D.get(k,v) | 키 k에 연결된 값을 반환하거나 키가 없으면 기본 값으로 v를 반환 |
| D.keys() | 딕셔너리 D의 키를 모은 객체를 반환 |
| D.values() | 딕셔너리 D의 값을 모은 객체를 반환 |
| D.items() | 딕셔너리 D의 키/값 쌍을 모은 객체를 반환 |
| D.pop(k) | 딕셔너리 D에서 키 k를 제거하고 연결됬던 값을 반환 (없으면 오류) |
| D.pop(k, v) | 딕셔너리 D에서 키 k를 제거하고 연결됬던 값을 반환 ( 없으면 v를 반환) |
| D.setdefault(k) | 딕셔너리 D에서 키 k 와 연결된 값을 반환 |
| D.setdefault(k, v) | 딕셔너리 D에서 키 k와 연결된 값을 반환 k가 D의 키가 아니면 값 v와 연결한 키 k를 D에 추가하고 v를 반환 |
| D.update(other) | other 내 각 키에 대해 D에 있는 키면 D에 있는 그 키의 값을 other에 있는 값으로 대체 other에 있는 각 키에 대해 D에 없는 키면 키/값 쌍을 D에 추가 |
D.clear()
D.get(key[,default])
person = {'name':'Alice', 'age':25}
print(person.get('name')) # Alice
print(person.get('country')) # None
print(person.get('country', 'Unknown')) # Unknown
D.keys()
D.values()
person = {'name' : 'Alice', 'age' : 25}
print(person.keys()) # dict_keys(['name', 'age'])
for v in person.values():
print(v)
"""
Alice
25
"""
D.items()
person = {'name': 'Alice', 'age':25}
print(person.itmes()) # dict_items([('name', 'Alice'), ('age', 25)])
for k, v in person.items():
print(k, v)
"""
name Alice
age 25
"""
D.pop(key[,default])
D.setdefault(key[,default])
person = {'name':'Alice', 'age':25}
print(pseron.setdefault('country', 'KOREA')) # KOREA
print(persion) # {'name': 'Alice', 'age':25, 'country': 'KOREA'}
D.update()
빠르게 이루어짐
[출처]https://mangkyu.tistory.com/102
임의의 크기를 가진 데이터를 고정된 크기의 고유한 값으로 변환하는 것
이렇게 생성된 고유한 값은 주로 해당 데이터를 식별하는 데 사용될 수 있음
파이썬에서는 해시 함수를 사용하여 데이터를 해시 값으로 변환하며, 이 해시 값은 정수로 표현됨
해시 함수
print(hash(1)) # 1
print(hash(1)) # 1
print(hash('a')) # 실행시마다 다름
print(hash('a')) # 실행시마다 다름
print(hash(1))
print(hash(1.0))
print(hash('1'))
print(hash((1,2,3)))
# TypeError : unhashable type: 'list
print(hash((1, 2, [3, 4])))
# TypeError: unhashable type: 'list'
print(hash([1, 2, 3]))
# TypeError: unhashable type : 'list'
my_set = {[1, 2, 3], 1, 2, 3, 4, 5}
# TypeError: unhashable type : 'set'
my_dict = {{3, 2}: 'a'}