python 문법정리

python 문법 정리

스페이스바를 기준으로 int 입력받기

1
a, b = map(int,input().split(' '))

enumerate

반복문 사용 시 몇 번째 반복문인지 확인이 필요할 때 사용

1
2
3
>>> t = [1, 5, 7, 33, 39, 52]
>>> for index, value in enumerate(t):
...     print(index,value)

문자열에서 특정 문자 제거

1
2
3
4
    string = input().replace(" ","",lens-1)
  
  # input : 1 0 1 0 0 0 1
  # output : 1010001

2진수

정수를 <0b> 가 앞에 붙은 이진 문자열로 변환한다.

1
2
3
bin(x)

>>> bin(3) = '0b11'

Set

중복을 없애고 싶을 때 사용

1
2
s = set("hello")
s2 => {e,h,l,o}

Zip

행령의 열을 모으고 싶을 때 사용

1
2
3
4
zip(*iterable)

>>> list(zip([1,2,3].[4,5,6]))
[(1,4),(2,5),(3,6)]

정규표현식

1
2
3
4
5
6
7
import re
re.findall() => 매칭되는 모든 경우를 리턴

>>> re.findall('\d+', file) => foo010bar020 -> ['010','020']

>>> re.split('[:. ]+', 'appli orange:banana tomato')
	-> ['appli', 'orange', 'banana', 'tomato']