π 리μ€νΈ
μ»΄ν리ν¨μ
λκ΄νΈ μμ 쑰건문과 λ°λ³΅λ¬Έμ μ μ©νμ¬ λ¦¬μ€νΈλ₯Ό μ΄κΈ°ννλ λ°©λ²
1
| a = [i for i in range(5)]
|
리μ€νΈμμ νΉμ κ°μ κ°μ§λ μμλ₯Ό λͺ¨λ μ κ±°νκΈ°
1
2
3
4
5
| arr = [1, 1, 2, 2, 3]
search = {1, 2}
# arrμ 1, 2λ₯Ό μ μΈν κ°λ§ μ μ₯
res = [a for a in arr if a not in search]
|
리μ€νΈ κ΄λ ¨ λ©μλμ μκ° λ³΅μ‘λ
| ν¨μλͺ
| μκ° λ³΅μ‘λ |
|---|
| append() | O(1) |
| sort() | O(NlogN) |
| reverse() | O(N) |
| insert() | O(N) |
| count() | O(N) |
| remove() | O(N) |
π νν
νΉμ§
- ννμ ν λ² μ μΈλ κ°μ λ³κ²½ν μ μλ€.
- ννμ 리μ€νΈμ λΉν΄ μλμ μΌλ‘ κ³΅κ° ν¨μ¨μ μ΄λ€.
- ννμ λΉλ‘―νμ¬ λ¦¬μ€νΈλ μμκ° μ‘΄μ¬νλ€.
ννμ μ¬μ©νλ©΄ μ’μ κ²½μ°
- μλ‘ λ€λ₯Έ μ±μ§μ λ°μ΄ν°λ₯Ό λ¬Άμ΄μ κ΄λ¦¬ν΄μΌ νλ κ²½μ°
- λ©λͺ¨λ¦¬λ₯Ό ν¨μ¨μ μΌλ‘ μ¬μ©ν΄μΌ νλ κ²½μ°
π μ§ν©
set()
- μ€λ³΅μ νμ©νμ§ μλλ€.
- μμκ° μ‘΄μ¬νμ§ μλλ€.
- μ¬μ μλ£νκ³Ό μ§ν© μλ£νμ μμκ° μκΈ° λλ¬Έμ κ²μν λ O(1)μ μκ° λ³΅μ‘λλ₯Ό κ°μ§λ€.
μ§ν© μλ£ν μ°μ°
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| s1 = {1, 2, 3}
s2 = {3, 4}
# ν©μ§ν©
print(s1 | s2)
print(s2.union(s1))
# κ΅μ§ν©
print(s1 & s2)
print(s1.intersection(s2))
# ν©μ§ν©
print(s1 - s2)
print(s1.difference(s2))
|
π ν¨μ
eval()
1
2
| # λ¬Έμμ΄ ννμ μμ κ³μ°ν΄μ£Όλ ν¨μ
print(eval("2+3"))
|
λ§μ½, νΌμ°μ°μ μμ 0μ΄ λΆμ΄ μλ κ²½μ°, μ€λ₯κ° λ°μνλ€. (Ex. 1+01)
π λΌμ΄λΈλ¬λ¦¬
1
2
3
4
5
6
7
8
9
10
11
| # μμ΄ : μλ‘ λ€λ₯Έ nκ°μμ μλ‘ λ€λ₯Έ rκ°λ₯Ό μ ννμ¬ λμ΄
from itertools import permutations
# μ‘°ν© : μλ‘ λ€λ₯Έ nκ°μμ μμμ μκ΄ μμ΄ μλ‘ λ€λ₯Έ rκ° μ ν
from itertools import combinations
# μ€λ³΅ μμ΄ : μ€λ³΅ κ°λ₯ν nκ°μμ rκ°λ₯Ό μ ννμ¬ λμ΄
from itertools import product
# μ€λ³΅ μ‘°ν© : μ€λ³΅ κ°λ₯ν nκ°μμ μμμ μκ΄ μμ΄ rκ° μ ν
from itertools import combinationswith_replacement
|
collections
1
2
3
4
5
6
7
8
9
10
11
| # μΉ΄μ΄ν
from collections import Counter
a = ['apple', 'banana', 'apple']
counter = Counter(a)
# μ
λ°μ΄νΈ
counter.update(a)
# κ°μ₯ λΉλκ° λμ μμΌλ‘ nκ° μΆλ ₯
print(counter.most_common(n))
|
1
2
3
4
5
6
7
8
| # ν ꡬν
# 리μ€νΈλ₯Ό μ¬μ©νλ κ²½μ°λ³΄λ€ ν¨μ¨μ±μ΄ λλ€.
from collections import deque
queue = deque()
queue.append(1)
queue.popleft()
|