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)
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>
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"])
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, 3, 4, 5}
- {0, 3, 1, 7}
- [1, 3, 4, 5]
- {3, 1, 7, 5}
- [3, 1, 7, 5]
-> ๋ต : 3
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
'๐ ์คํฐ๋ > ํ์ด์ฌ ์คํฐ๋ ๊ณผ์ ์ ์ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[3ํ/์์์ฐ] 7์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ์๋ฃ๊ตฌ์กฐ (0) | 2023.05.17 |
---|---|
[3ํ/๊น์๊ฒฝ] 7์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ์๋ฃ๊ตฌ์กฐ (0) | 2023.05.17 |
[4ํ / ๊น์ ๊ฒฝ] 6์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ๋ฌธ์์ด (0) | 2023.05.10 |
[1ํ / ๊ถ๋จ์] 6์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ๋ฌธ์์ด (0) | 2023.05.10 |
[3ํ/ ๊น์๊ฒฝ] 6์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ๋ฌธ์์ด (0) | 2023.05.10 |