로그인 바로가기 하위 메뉴 바로가기 본문 바로가기

파이썬 자료구조

임시 이미지 커넥트재단 edwith
http://www.edwith.org/python-data/forum/25080
좋아요 1146 수강생 13920

fname = input('Enter file: ')
if len(fname) < 1 : fname = 'clown.txt'
hand = open(fname)


for line in hand :
if len(line) < 2 : continue
spl = line.split()

dic = dict()
for w in spl :
dic[w] = dic.get(w,0) + 1

 

bigcount = 0
bigname = None
for name,count in dic.items() :
if bigcount < count :
bigcount = count
bigname = name

print(bigname, bigcount)


위에 강조한 dic = dict()  부분만 for 루프 위로 올리면 결과가 옳게 나오는데, 왜 dic이 제가 표기한 저 위치에 놓으면 took 1 로만 나오는지 모르겠습니다. 옳게 나오면 the 226입니다.



왜 저 위치에 dict()를 두면 결과가 다르게 나오나요?