[ํ๋ก๊ทธ๋๋จธ์ค] LV.1 ์ ๋ฆฌ
์ฌ๋ผ์ด์ฑ
https://twpower.github.io/119-python-list-slicing-examples
๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์์๊ฐ์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
# ์ธ๋ฑ์ค ์์์ ๋ฐ๋ฅธ ๋ฐฐ์ด๊ฐ ์ ์ฅ
rank = [6,6,5,4,3,2,1]
# ๋ฐฐ์ด์ด ๋ฐ๋ณต๋๋ ๊ฒฝ์ฐ ์ธ๋ฑ์ค๋ฅผ ๋๋จธ์ง ๊ฐ์ผ๋ก ์ค์ ํด์ ๋ฐ๋ณต๊ฐ๋ฅ
count(n)
lottos.count(0)
๋ฌธ์์ด
# ์์น return
find('a') // ์์ผ๋ฉด -1
index('a') // ์์ผ๋ฉด ์ค๋ฅ
# ๋ฌธ์์ด ์ฝ์
",".join('abcb') -> 'a,b,c,d'
"".join(['a','b','c'] -> 'abc'
# ๋๋ฌธ์ ์๋ฌธ์ ๋ณํ
upper(), lower()
# ์ผ์ชฝ / ์ค๋ฅธ์ชฝ / ์์ชฝ ๊ณต๋ฐฑ ์ง์ฐ๊ธฐ
lstrip(), rstrip(), strip()
# ๋ฌธ์์ด ์ ๊ฑฐ
txt.strip('a')
# ์ ์๋ฌธ์ / ํํ๋ฌธ์ ์ ๊ฑฐ
txt.lstrip('https://')
txt.rstrip('.net')
๋ฆฌ์คํธ
# ๋ฆฌ์คํธ ์ถ๊ฐ (์ ์ผ ๋ค์ / ํน์ index์)
.append(a)
.insert(index, a)
# ๋ฆฌ์คํธ ๋ํ๊ธฐ
.extend(a)
# ๋ฆฌ์คํธ ์ญ์
txt.remove(a)
del txt[txt.index(3)]
remove๋ณด๋ค ์ฌ๋ผ์ด์ฑ ์ ๊ทน ํ์ฉํ๊ธฐ
[indexํฌํจ : index๋ฏธํฌํจ(๋ฐ๋ก์๊น์ง)]
if answer[-1] == '.':
answer = answer[:-1]
isalpha(), isdigit(), is_integer(), i in txt
if txt.isalpha()
if txt.isdigit()
if txt.is_integer()
if txt in ['1', '2', '3']
replace (๋ฌธ์์ด๋ง ๊ฐ๋ฅ)
while '..' in answer:
answer = answer.replace('..', '.)
dictionary
numdic = {"zero" : "0", "one" : "1"}
def solution(answer):
for key, value in numdic.items():
answer = answer.replace(key, value)
return int(answer)
OR
for i in range(len(numdic)):
answer = answer.replace(numdic[i], str(i))
return int(answer)
# ๊ฐ์ถ๊ฐ
a['name'] = 'pey'
# ๊ฐ์ญ์
del a[1]
# key๋ฅผ ํตํด value ์ป๊ธฐ
a[key] -> value // ์์ผ๋ฉด ์ค๋ฅ ๋ฐ์
a.get('key') // ์์ผ๋ฉด None return
# key ๋ฆฌ์คํธ
a.keys()
for key in a:
print(dic)
# value ๋ฆฌ์คํธ
a.values
for val in a.values()
print(val)
# key value ์
a.items()
# key: value ์ ๋ชจ๋ ์ง์ฐ๊ธฐ
a.clear()
# ํด๋น key๊ฐ ๋์
๋๋ฆฌ์ ์๋์ง ์กฐ์ฌํ๊ธฐ
'key' in a
-> True or False
๋ด์ฅํจ์ zip
list(zip([1,2,3], [4,5,6]))
[(1,4), (2,5), (3,6)]
sort(), sorted(a) (์ํ๋ฒณ๋ ๊ฐ๋ฅ)
#sort
list.sort()
#sorted
sorted(list) // nonetype error ๋ฐ์ X, ๊ฐ ๋์
๊ฐ๋ฅ
#reverse (์ญ์)
list = sorted(A, reverse = True)
์งํฉ
set(A) - set(B)
๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ฆฌ์คํธ / ๋์ ๋๋ฆฌ ๋ฐ๋ก ์์ฑ
list = [i for i in A if i not in B]
dic = {id: [] for id in list}
lambda
#lambda
(lambda: x + 10)(1) = 11
#map
list(map(lambda x: x+10, [1,2,3])
#list sorted
list = sorted(a, key = lambda x: result[x], reverse = True)
rjust / ljust / zfill (์ ๋ ฌ + ์ฑ์)
#rjust ์ค๋ฅธ์ชฝ ์ ๋ ฌ
list.rjust(5,"0")
#ljust ์ผ์ชฝ ์ ๋ ฌ
list.ljust(5,"a")
#zfill (์ค๋ฅธ์ชฝ ์ ๋ ฌ ํ 0์ ์๋์ผ๋ก ์ฑ์์ค)
list.zfill(3)
*์ค์ (์ธ๋ฑ์ค -1:)
[-1:] == [i]์์ ์ธ๋ฑ์ค๋ฅผ -1:์ผ๋ก ํ๋ฉด ๋น ์ธ๋ฑ์ค๋ผ๋ ๋น๊ต๊ฐ๋ฅ (์๋ฌX)
๊ฐ ๋ณ๊ฒฝ
a,b = b,a
ํฌ๋งทํ
s = "๊น์๋ฐฉ์ %d์ ์๋ค." %seoul.index("Kim")
๊ธฐํ
#boolean
True, False (๋๋ฌธ์)
#์ฐ์ฐ
/ (๋ชซ, ๋๋จธ์ง)
// (๋ชซ)
% (๋๋จธ์ง)
i**5 (์ ๊ณฑ)
i**0.5 (๋ฃจํธ)
#์์ํ๋ณ๋ฒ
ํด๋น ๋ฒ์ ์ซ์๋ก ์์ ์ ๋๋ด์๋ ๋๋จธ์ง๊ฐ 0์ด ์๋ ๋
๊ฐ % (2) ~ (๊ฐ-1) != 0
#collections
import collections
Counter(list) -> list์์ ๋ ๊ฐ๋ค ๊ฐ๊ฐ ๋ช๊ฐ์ธ์ง dic์ผ๋ก ๋ฐํ
์ญ์ ์ถ๋ ฅ (๋ฆฌ์คํธ, ๋ฌธ์์ด)
answer1 = '1234'
answer2 = [1,2,3,4]
answer1[::-1]
answer2[::-1]
์ด๊ธฐํ
n, k = map(int, input().split())
arr = [int(input()) for _ in range(n)]
num = list(map(int, input().split()))
dp = [0 for i in range(k+1)]
์๊ฐ๋ณต์ก๋
'๐ป Coding Problems Solving > Hack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ์ ๊ท์์ด๋ ์ถ์ฒ์ผ๋ก ์์๋ณด๋ regex (0) | 2022.10.14 |
---|---|
[JAVA] ๋คํธ๊ฒ์์ ํตํด ์์๋ณด๋ char ๋ฌธ์/์ซ์ ํ์ธ (0) | 2022.10.14 |
[JAVA] 2์ง์, 8์ง์, 16์ง์ + ์ฑ์๋ฃ๊ธฐ! (0) | 2022.10.13 |
[JAVA] ์์ ์ํธ (0) | 2022.10.13 |
[JAVA] ์ฝ๋ฉํ ์คํธ ์ ์ฉํ ์ฝ๋ ๋ชจ์ (0) | 2022.10.07 |
์ต๊ทผ๋๊ธ