알고리즘(Python)

[Python] 자주쓰는 라이브러리, 함수 정리

Gayeon 2021. 7. 8. 21:51

Python 라이브러리, 함수 정리

  • 절댓값 - abs()

  • 제곱근 - math.sqrt() (math 라이브러리 필요)

  • 거듭제곱 - math.pow(x,y) or x**y (x의 y승)

  • 아스키 코드 변환 - ord(문자), chr(숫자)

  • 리스트 속 값 인덱스 찾기 - list.index('값')

  • 배열 속 문자 개수 셀때 - list.count(값)

  • 배열 속 문자 삭제
    • list.remove('값') : 같은 값 처음발견된 하나만 지워줌
    • del list[index] : 특정 인덱스 삭제
    • list.pop(index) : 특정 인덱스 삭제 - 인덱스 생략 시 제일 마지막원소 삭제

  • 정렬 특정기준 만들때 - list = sorted(list,key=lambda x:x[1]) >두번째 값 기준정렬

  • 개행문자 제거 - .rstrip()

  • 숫자인가? - .isdigit(), 영어인가 - .isalpha()

  • 리스트 속 특정값 존재여부 - x in list >boolean 값

  • 값 두개 체인지 - a,b = b,a

  • 서로 영향안받게 복사하기 
    • import copy, a = copy.deepcopy(b)
    • 슬라이싱 (list =list2[:])

  • 순열, 조합 쌍 구하기
    • from itertools import combinations
    • combinations(n,r) → nCr
    • from itertools import permutations
    • permutations(n,r) → nP
    • combinations_with_replacement(arr,r) //반복가능

  • set() - 중복없애주는 집합, 추가할 때 append아니고 add로 사용

  • from collections import Counter

  • Counter(list)- 배열 원소들의 중복개수를 딕셔너리로 반환해줌.

'알고리즘(Python)' 카테고리의 다른 글

[Python] 시간복잡도, 공간복잡도  (0) 2021.07.08