이번 주 까지는 기본적인 파이썬의 자료구조에 대해 학습하였다.


1_1 first trial

음…

'''
M = "aaa bbb aaa ccc bbb bbb"
counts={'aaa':0,'bbb':0.'ccc':0,'ddd':0,'eee':0}
print("M =", M)

words = M.split()

for word in words:
    if (word=='aaa'):
        counts[word] += 1
    else if (word=='aaa'):
        counts[word] += 1
    else if (word=='bbb'):
        counts[word] += 1
    else if (word=='ccc'):
        counts[word] += 1
    else if (word=='ddd'):
        counts[word] += 1
     else if (word=='eee'):
        counts[word] += 1
    
    for word in counts:
        if (count[word]>0)
        print(word, count[word])
'''
#문제는 value를 정렬해야 한다는 것. 오타는 수정하자...
M = "aaa bbb aaa ccc bbb bbb"
counts={'aaa':0,'bbb':0,'ccc':0,'ddd':0,'eee':0}
print("M =", M)

words = M.split()

for word in words:
    if (word=='aaa'):
        counts[word] += 1
    elif (word=='bbb'):
        counts[word] += 1
    elif (word=='ccc'):
        counts[word] += 1
    elif (word=='ddd'):
        counts[word] += 1
    elif (word=='eee'):
        counts[word] += 1

for word in counts:
    if counts[word]>0:
      print(word, counts[word])

#정렬을 고려한다면 결국 lambda 함수를 사용하여야 한다
M = "aaa bbb aaa ccc bbb bbb"
counts={'aaa':0,'bbb':0,'ccc':0,'ddd':0,'eee':0}
print("M =", M)

words = M.split()

for word in words:
    if (word=='aaa'):
        counts[word] += 1
    elif (word=='bbb'):
        counts[word] += 1
    elif (word=='ccc'):
        counts[word] += 1
    elif (word=='ddd'):
        counts[word] += 1
    elif (word=='eee'):
        counts[word] += 1

count_final=sorted(counts.items(), key=lambda word: word[1], reverse=True)
print(count_final)
break

1_1 second trial