Python - Collection의 defaultdict 에 관한 공부

default dict는 dictionary를 생성할 때 아주 유용한 모듈. d = defaultdict(any_def, (기본값으로 쓸)key=value) d[any_key] => d[any_key]를 호출하면 1) 이미 any_key에 해당하는 value 값이 있을 땐 값을 return 2) any_key 값이 없을 땐 새로이 any_key라는 key를 배정하면서 'any_def' 실행의 결과값을 value로 set함 EXAMPLE )))) def hello(): return "world" d = defaultdict(hello, a=[1,2,3]) d() ==> d = {a: [1,2,3]} d["depth1"] ==> d = {a:[1,2,3], "depth1" : "world"} d["depth1"] = "depth1_value" ==> d = {a:[1,2,3], "depth1" : "depth1_value"}

댓글

이 블로그의 인기 게시물

로컬 Tomcat 서버 실행속도 개선

2019.05.23 - SQLAlchemy 의 객체 상태 관리 (expire, refresh, flush, commit) 에 관한 이해

2020.02.17 Python의 multiprocessing 중 Pool.map(), chunksize에 관한 내용