enumerate, set, Counter(from collections), ceil(from math)

Ruah·2024년 9월 15일

theory

목록 보기
6/11

enumerate()

  • 리스트나 다른 반복 가능한 객체에서 인덱스와 값을 함께 반환.
 list = [ 'a', 'b', 'c' ]
 for index, value in enumerate(list):
 	print(indes, value)
   
 # 출력 :
 0 a
 1 b
 2 c

set()

  • 집합 자료형을 만들어 중복제거, 순서 없이 저장

    list = [ 1, 2, 2, 3, 3, 4 ]
    my_set = set(list)
    print(my_set)
    
    # 출력 : { 1, 2, 3, 4 }

Counter(from collections)

  • 리스트나 반복 가능한 객체에서 각 요소가 몇 번 등장햇는지를 카운트
from collections import Counter
list = [ 1, 2, 2, 3, 3, 3 ]
count = Counter(list)
print(count)

# 출력 : Counter({3:3, 2:2, 1:1})
# 3이 3개, 2가 2개, 1이 1개

math.ceil() (from math)

  • 소수점이 있는 숫자를 올림하여 가장 가까운 정수를 반환하는 함수
  • 나눗셈의 결과에서 소수점을 올림 처리할 때 사용. 예를 들어, 계산 결과가 4.2일 때 정수로반환해야하는 경우.
import math

print(math.ceil(4.2)) # 출력 : 5
print(math.ceil(5.8)) # 출력 : 6
print(math.ceil(5.0)) # 출력 : 5
profile
집요한 주니어 개발자의 호되게 당했던 기록

0개의 댓글