스터디/파이썬 스터디 과제

[4팀/김윤] 7차시 파이썬 과제 - 자료구조

알 수 없는 사용자 2023. 5. 15. 21:13

1. ~ 3.

딕셔너리와 collections 모듈을 이용하여 텍스트 마이닝 프로그램을 만들어보려 한다.

텍스트 마이닝이란 텍스트를 분석하여 의미 있는 결과를 도출하는 과정을 말한다. 일반적으로 텍스트 마이닝을 할 때 각 문장에서 단어가 얼마나 많이 출현하는지 분석한다.

이때 defaultdict 모듈을 사용하면 문장에 있는 단어의 개수를 쉽게 파악할 수 있다.

다음 문장에 있는 단어의 개수를 파악해보자.


A press release is the quickest and easiest way to get free publicity. If well written, a press release can result in multiple published articles about your firm and its products. And that can mean new prospects contacting you asking you to sell to them.


프로그램의 규칙은 다음과 같다.

    - 문장의 단어 개수를 파악하는 코드를 작성한다.

    - defaultdict 모듈을 사용한다.

    - 단어의 출현 횟수를 기준으로 정렬된 결과를 보여주기 위해 orderdict 모듈을 사용한다.

 

1. 함수 2개를 사용하여 주어진 텍스트 문장을 분석하기 위한 형태로 변환시키는 코드를 1행 작성하시오.

    - 소문자로 바꿀 것

    - 단어 단위로 자를 것

-> 답 :

text = text.lower()
text = text.split()

 

2. defaultdict 모듈을 사용해 단어의 개수를 세어주는 코드를 완성하시오

text = ...

from collections import defaultdict

word_count = _____________
for word in text:
	________________

-> 답 :

from collections import defaultdict

word_count = defaultdict(lambda : 0)
for word in text:
    word_count[word] += 1

 

3. 단어의 출현 횟수를 기준으로 정렬된 결과를 보여주려 한다. OrderedDict 모듈을 사용하여 코드를 완성하시오.

text = ...

from collections import defaultdict

word_count = _____________
for word in text:
	________________
   
from collections import OrderedDict
for i, v in _____________:
	print(i, v)

-> 답 : 

def sort_by_key(t):
    return t[0]

from collections import OrderedDict

word_count = OrderedDict(word_count)

for key, value in OrderedDict(sorted(word_count.items(), key=sort_by_key)).items():
    print(key, value)

#1 ~ #3 실행결과

 

4. 다음과 같이 코드를 작성했을 때, 실행 결과를 작성하고, 다음 코드에서 알 수 있는 딕셔너리 구조의 특징에 대해 서술하시오.

score_dict = {"Kim" : 80, "Lee" : 85, "Ahn" : 83, "Choi" : 90}
first_key = list(score.dict.keys())[0]
score_dict[first_key] = 90
print(score_dict.values())

-> 답 : <built-in method values of dict object at 0x1084b7300>

#4 실행결과

 

5. 다음 코드를 실행시켜 아래와 같은 실행 결과를 얻으려 한다. 빈 칸에 맞지 않는 코드를 모두 고르시오.

from collections import deque

deque_list = deque(["a", "b", "c"])
_____________________
print(deque_list)

# 실행결과 deque(["c", "a", "b"])
  • deque_list.rotate(1)
  • deque_list.rotate(-2)
  • deque_list.appendleft("c")
  • deque_list = deque(["c", "a", "b"])
  • deque_list = deque(["a", "b", "c"])

-> 답 : deque_list.appendleft("c"), deque_list = deque(["a", "b", "c"])

 

6. 5번의 맞지 않는 코드의 실행 결과를 각각 작성하시오.

-> 답 :

deque_list.appendleft("c")

    -> 실행 결과 : deque(["c", "a", "b", "c"])

deque_list = deque(["a", "b", "c"])

    -> 실행 결과 : deque(["a", "b", "c"])

#5 ~ #6 실행결과 1
#5 ~ #6 실행결과 2

 

 

7. 다음과 같이 코드를 작성했을 때, 실행 결과로 알맞은 것은?

def quiz_2(list_data):
	a = set(list_data)
    return (list(a)[1:5])

list_1 = [0, 3, 1, 7, 5, 0, 5, 8, 0, 4]

print(quiz_2(list_1))
  1. {1, 3, 4, 5}
  2. {0, 3, 1, 7}
  3. [1, 3, 4, 5]
  4. {3, 1, 7, 5}
  5. [3, 1, 7, 5]

-> 답 : 3

#7 실행결과

 

8. 다음과 같이 딕셔너리가 선언되었을 때, 각 코드의 실행 결과를 쓰시오.

country_code = {"America" : 1, "Korea" : 82, "China" : 86, "Japan" : 81}
country_code.values()
[    1    ]

country_code = {"America" : 1, "Korea" : 82, "China" : 86, "Japan" : 81}
country_code
[    2    ]

country_code = {"America" : 1, "Korea" : 82, "China" : 86, "Japan" : 81}
country_code.keys()
[    3    ]

country_code = {"America" : 1, "Korea" : 82, "China" : 86, "Japan" : 81}
85 in country_code.values
[    4    ]

country_code = {"America" : 1, "Korea" : 82, "China" : 86, "Japan" : 81}
"Korea" in country_code.keys()
[    5    ]

-> 답 :

    1 = {'America': 1, 'Korea': 82, 'China': 86, 'Japan': 81}

    2 = {'America': 1, 'Korea': 82, 'China': 86, 'Japan': 81}

    3 = dict_keys(['America', 'Korea', 'China', 'Japan'])

    4 = False

    5 = True

#8 실행결과1
#8 실행결과 2