Python - defaultdict
by yy on 2008/01/08 16:45
defaultdict는 파이썬 2.5 버전부터 collections에 새로 추가된 자료형입니다. dictionary대신 쓰면 좋습니다. 아실만한 분들은 다들 아시겠지만, syntaxhighlighter 테스트도 해볼겸 올립니다. ㅎㅎ
디폴트값을 지정해 줄 수 있는 루비의 Hash같은 역할을 합니다.
보통 네트워크 구조를 가지고 뭘 할때, 전 네트워크 구조를 dictionary로 저장합니다. 각 노드들이 key가 되고 그 노드들의 이웃 노드들의 set이 value가 되는 dictionary죠. a라는 노드에서 b라는 노드로 향하는 링크를 만드려면
이렇게 하면 됩니다. python 2.5가 나오기 전에는 setdefault()를 쓰면 if문을 없애고 좀 더 깔끔하게 할 수 있었습니다.
하지만 가장 깔끔하고 빠른 방법은 defaultdict를 쓰는 것입니다.
데이터로부터 분포를 얻을 때도 유용하게 쓰일 수 있습니다.
참고
TRACKBACK :: http://www.yongyeol.com/blog/trackback/17
-
Ambien high.
Tracked from Ambien high. 삭제Ambien high.
2010/09/04 03:43 -
Adderall.
Tracked from Adderall dosage instructions. 삭제Adderall xr side effects. Adderall. Adderall without a prescription. Who makes adderall.
2010/09/07 16:56