๐ฏ string์ 10์ง๋ฒ์ผ๋ก
ํ์ด์ฌ์ int(x, base=10) ํจ์๋ 10์ง๋ฒ์ผ๋ก ๋ณํํด์ค๋ค.
num = '0001'
base = 2
answer = int(num, base) #1
๐ฏ ๋ฌธ์์ด ์ ๋ ฌํ๊ธฐ - ljust, center, rjust
๋ฌธ์์ด s๋ฅผ ์ข์ธก/๊ฐ์ด๋ฐ/์ฐ์ธก ์ ๋ ฌํ์ฌ n ๊ธธ์ด์ ๋ฌธ์์ด์ ๋ง๋ ๋ค.
s = '๊ฐ๋๋ค๋ผ'
n = 7
s.ljust(n) # '๊ฐ๋๋ค๋ผ ' # ์ข์ธก ์ ๋ ฌ
s.center(n) # ' ๊ฐ๋๋ค๋ผ ' # ๊ฐ์ด๋ฐ ์ ๋ ฌ
s.rjust(n) # ' ๊ฐ๋๋ค๋ผ' # ์ฐ์ธก ์ ๋ ฌ
๐ฏ ๋ชจ๋ ์๋ฌธ์, ๋๋ฌธ์, ๋์๋ฌธ์, ์ซ์ ๊ฐ์ ธ์ค๊ธฐ
import string
string.ascii_lowercase # ์๋ฌธ์ abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase # ๋๋ฌธ์ ABCDEFGHIJKLMNOPQRSTUVWXYZ
string.ascii_letters #๋์๋ฌธ์ ๋ชจ๋ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
string.digits # ์ซ์ 0123456789
๐ฏ zip ํจ์ - ์ฌ๋ฌ๊ฐ์ Iterable ๋์์ ์ํ
list1 = [1, 2, 3, 4]
list2 = [100, 120, 30, 300]
list3 = [392, 2, 33, 1]
answer = []
for i, j, k in zip(list1, list2, list3):
print( i + j + k )
๐ฏ zip ํจ์ - ๋ฆฌ์คํธ์์ ๋์ ๋๋ฆฌ ์์ฑ
animals = ['cat', 'dog', 'lion']
sounds = ['meow', 'woof', 'roar']
answer = dict(zip(animals, sounds)) # {'cat': 'meow', 'dog': 'woof', 'lion': 'roar'}
๐ฏ map - ๋ชจ๋ ๋ฉค๋ฒ์ type ๋ณํํ๊ธฐ
list1 = ['1', '100', '33']
list2 = list(map(int, list1))
๐ฏ join - ๋ฉค๋ฒ๋ฅผ ํ๋๋ก ์ด์ด๋ถ์ด๊ธฐ
my_list = ['1', '100', '33']
answer = ''.join(my_list)
print(answer) # '110033'
๐ฏ product - ๊ณฑ์งํฉ ๊ตฌํ๊ธฐ
from itertools import product
iterable1 = 'ABCD'
iterable2 = 'xy'
print(list(product(iterable1, iterable2)))
# [('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y'), ('D', 'x'), ('D', 'y')]
๐ฏ permutations - ์์ด ๋ง๋ค๊ธฐ
from itertools import permutations
pool = ['A', 'B', 'C']
# 3๊ฐ์ ์์๋ก ์์ด ๋ง๋ค๊ธฐ
print(list(map(''.join, permutations(pool)))) #['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
# 2๊ฐ์ ์์๋ก ์์ด ๋ง๋ค๊ธฐ
print(list(map(''.join, permutations(pool, 2)))) #['AB', 'AC', 'BA', 'BC', 'CA', 'CB']
๐ฏ Counter - ์ด๋ค ์์๊ฐ ๋ช๋ฒ ๋ฑ์ฅํ๋์ง
import collections
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 7, 9, 1, 2, 3, 3, 5, 2, 6, 8, 9, 0, 1, 1, 4, 7, 0]
answer = collections.Counter(my_list)
print(answer) # Counter({1: 4, 2: 3, 3: 3, 7: 3, 4: 2, 5: 2, 6: 2, 8: 2, 9: 2, 0: 2})
print(answer[1]) # 4
print(answer[3]) # 3
print(answer[100]) # 0
๐ฏ startswith, endswith - ์์ํ๋ ๋ฌธ์, ๋๋๋ ๋ฌธ์ ์ฌ๋ถ
s = '12345'
s.startswith('123')
# True
s.startswith('34')
#False
s.startswith('34', 2)
#True
'Language > python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ์ ๊ท ํํ์ re (0) | 2020.05.05 |
---|---|
[python] bisect / ์ด์ง ํ์ ๋ด์ฅํจ์ (0) | 2020.03.26 |
[python] ๋ฆฌ์คํธ ์ ๋ฆฌ (0) | 2020.01.29 |
[python] ๊ธฐ์ด ๋ฌธ๋ฒ ์ ๋ฆฌ (0) | 2020.01.25 |