본문 바로가기

IT-Consultant

코드의 간결화

지금까지 많은 언어를 접했지만 이것 만큼 간결한게 없다.
아래 코드가 도대체 어떤 역활을 하는 것일까?
prefs 사전을 person1에 대해서 for loop를 돌고 person2과 공통된게 있을때만 pow 함수를 실행한다.

sum([pow(prefs[person1][item]-prefs[person2][item],2)
                      for item in prefs[person1] if item in prefs[person2]])