:컬렉션을 반복하는 동안 컬렉션을 수정할 때 위험이 있습니까?
co_occurrences[word][file_name] = occurrence_count/co_occurrences["__length"][file_name]
위험 :
co_occurrences = defaultdict(lambda: defaultdict(int))
# Populate the dictionary...
for word, occurrence_vector in co_occurrences:
if word == "__length": continue
for file_name, occurrence_count in occurrence_vector:
co_occurrences[word][file_name] = occurrence_count/co_occurrences["__length"][file_name]
이 라인인가? 위험한 것은 각 키를 한 번만 반복하려는 것이므로이 동작을 수정하는 모든 코드는 위험합니다. 내가 반복하는 데이터 구조를 수정하고 있기 때문에 그럴 것 같아.
co_occurrences.keys() : for_word = co_occurrences [word]'for for word를 사용하여이 키의 사본을 만들고 그 대신 반복 할 수 있습니다. – Perkins
또한 당신의 dict에 magic entry''__length ''를 갖는 것은 정말로 이상합니다. 파일 이름을 길이에 매핑하는'file_length' dict를 만드십시오. – U2EF1
관련 항목 : http://stackoverflow.com/a/2315529/846892 –