Python 기억할 내용들

김지원·2022년 12월 1일
0

Python 공부하기

목록 보기
2/3

set()

s1 = set([1,2,3,4,5])
s2 = set([3,4,5,6,7])

# 합집합
u = s1.union(s2)
u = s1 | s2

# 교집합
i = s1.intersection(s2)
i = s1 & s2

# 차집합
d = s1.difference(s2)
d = s1 - s2

RainbowCSV

collections 모듈

  • List, Tuple, Dict에 대한 Python Built-in 확장 자료 구조 (모듈)
from collections import deque
from collections import Counter
from collections import OrderedDict  # 현재는 dict도 ordered됨
from collections import defaultdict
from collections import namedtuple

deque

  • rotate, reverse 등 Linked List의 특성을 지원함
  • 효율적 메모리 구조로 처리 속도 향상

defaultdict

  • Dict type 값에 기본 값을 지정, 신규값 생성시 사용하는 방법
d = defaultdict(lamba: 0)

namedtuple

  • Tuple 형태로 Data 구조체를 저장하는 방법
  • 저장되는 data의 vairable을 사전에 지정해서 저장함
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22)
print(p[0] + p[1])

x, y = p
print(x, y)
print(p.x, p.y)
print(Point(x=11, y=22))
profile
Make your lives Extraordinary!

0개의 댓글