[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 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

ํฌ๋งทํŒ…

https://lcs1245.tistory.com/entry/Python-%EB%AC%B8%EC%9E%90%EC%97%B4-%ED%8F%AC%EB%A7%A4%ED%8C%85-%ED%95%98%EA%B8%B0-format-f-string

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)]

 

์‹œ๊ฐ„๋ณต์žก๋„

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ