221027 TIL
๋ฐฑ์ค
Class2
- [Bronze1] 2609๋ฒ_์ต๋๊ณต์ฝ์ ์ต์๊ณต๋ฐฐ์
- [Silver2] 2805๋ฒ_๋๋ฌด ์๋ฅด๊ธฐ
- [Bronze3] 4153๋ฒ_์ง๊ฐ์ผ๊ฐํ
- [Silver4] 10816๋ฒ_์ซ์ ์นด๋2
- [Silver4] 10866๋ฒ_๋ฑ
- [Bronze1] 11050๋ฒ_์ดํญ ๊ณ์1
- [Bronze2] 15829๋ฒ_Hashing
- [Silver2] 18111๋ฒ_๋ง์ธํฌ๋ํํธ [์คํจ]
ํ๋ก๊ทธ๋๋จธ์ค
Level1
- [2018 KAKAO BLIND RECRUITMENT]_[1์ฐจ] ๋คํธ๊ฒ์
Level2
CS
-
์ค๋ ๋ฐฐ์ด ๋ด์ฉ
๋์ ๋๋ฆฌ
๋ฐ์ดํฐ๋ฅผ key์ value๊ฐ ๋์๋๋ ํํ๋ก ํ๋์ ๋ณ์์ ์ ์ธํ๋ ์๋ฃํ์ด๋ค.
๋์ ๋๋ฆฌ๋ ์ค๊ดํธ{}๋ฅผ ์ด์ฉํ์ฌ {key: value} ํํ๋ก ์ ์ธํด์ฃผ๋ฉด ๋๋ค. ๋ํ ๊ฐ๊ฐ์ ์์ ์ฝค๋ง(,)๋ก ๊ตฌ๋ถํ๋ค. key์๋ ๋ณํ์ง ์๋ ๊ฐ์ ์จ์ฃผ๋ฉฐ ์ค๋ณต์ด ๋ถ๊ฐ๋ฅํ๋ค. ๋ฐ๋ฉด value์๋ ๋ณํ๋ ๊ฐ๊ณผ ๋ณํ์ง ์๋ ๊ฐ ๋ชจ๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋์ ๋๋ฆฌ ์ฌ์ฉ ๋ฐฉ๋ฒ
- key๋ฅผ ์ด์ฉํ์ฌ ๊ทธ์ ํด๋นํ๋ value๋ฅผ ์ป๋ ๋ฐฉ์์ผ๋ก ๊ฐ์ ์ถ๋ ฅ
- get() ์ฌ์ฉํ์ฌ ์ถ๋ ฅ
- values() ์ฌ์ฉํ์ฌ ๋ชจ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
2) key ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
- dict.keys()
3) key / value ๊ฐ์ ธ์ค๊ธฐ
- dict.items()
1) value ์ถ๋ ฅ ๋ฐฉ๋ฒ
### value ์ถ๋ ฅ ๋ฐฉ๋ฒ
# 1. key ์ฌ์ฉํ์ฌ ์ถ๋ ฅ
dict = {'A': ['a','b','c'], 'B': ['d','e']}
dict['A']
>> ['a','b','c']
# 2. get ์ฌ์ฉํ์ฌ ์ถ๋ ฅ
dict = {'A': ['a','b','c'], 'B': ['d','e']}
dict.get('A')
>> ['a','b','c']
# 3. values()
dict = {'A': ['a','b','c'], 'B': ['d','e']}
dict.values()
>> dict_values([['a','b','c'], ['d','e']])
### key ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
# 1. keys()
dict = {'A': ['a','b','c'], 'B': ['d','e']}
dict.keys()
>> dict_keys(['A','B'])
### ๋์
๋๋ฆฌ ์ ์ถ๊ฐํ๊ธฐ
dict[key] = value
### ๋์
๋๋ฆฌ ์์ ์ญ์ ํ๊ธฐ
dict = {'A': ['a','b','c'], 'B': ['d','e']}
del dict['A']
>> {'B': ['d','e']}
### ๋์
๋๋ฆฌ key์ value๋ฅผ ์์ผ๋ก ์ถ๋ ฅํ ๋
# items()
dict = {'A': ['a','b','c'], 'B': ['d','e']}
dict.items()
>> dict_items([('A', ['a','b','c']), ('B', ['d','e'])])
- Counter ํจ์
- most_common() : Counter()์์ ๊ฐ์ฅ ๋น๋์๊ฐ ๋์ ์์ผ๋ก ํ์ํด ์ฃผ๋ ํจ์
- ์ธ์๊ฐ์ผ๋ก ์ซ์๋ฅผ ์ ๋ ฅํ๋ฉด ๊ทธ ์ซ์์ธ๋ฑ์ค ๊น์ง์ ๋น๋์๋ฅผ ํ์
- element() : Counter() ์์ฑ๋ ๋์ ๋๋ฆฌ๋ฅผ ๊ฐ ๋ฉค๋ฒ๋ค๋ก ๋ง๋ค์ด ๋ฐํ ํ๋ ๊ฒ
- most_common() : Counter()์์ ๊ฐ์ฅ ๋น๋์๊ฐ ๋์ ์์ผ๋ก ํ์ํด ์ฃผ๋ ํจ์
from collections import Counter
value = 'Hello world'
count = Counter(value)
print(count.most_common())
>> [('l', 3), ('o', 2), ('H', 1), ('e', 1), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]
-----------------------------------------------------------------------------------------
value = 'Hello world'
count = Counter(value)
print(list(count.elements()))
>> ['H', 'e', 'l', 'l', 'l', 'o', 'o', ' ', 'w', 'r', 'd']
- factorial ํจ์
- math ๋ชจ๋์ ๋ด์ฅ๋์ด ์๋ factorial ํจ์
- factorial์ for๋ฌธ๊ณผ ์ฌ๊ท๋ฅผ ํตํด ๊ตฌํ
import math
factorial(n)
# ์ฌ๊ท๋ฅผ ํตํ ๊ตฌํ
def factorial(n):
if (n>1):
return n * factorial(n-1)
else:
return 1
# for๋ฌธ์ ํตํ ๊ตฌํ
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
'๐ฉ TIL[Today I Learned' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ 221031 TIL[Today I Learned] (0) | 2022.10.31 |
---|---|
๐ฉ 221030 TIL[Today I Learned] (0) | 2022.10.31 |
๐ฉ 221029 TIL[Today I Learned] (0) | 2022.10.30 |
๐ฉ 221028 TIL[Today I Learned] (0) | 2022.10.28 |
๐ฉ 221026 TIL[Today I Learned] (0) | 2022.10.28 |