[Python] 30초 파이썬 - chunk

리미·2020년 4월 24일
1

30 Seconds of Python

목록 보기
1/5

Chunk

list형식을 입력받아 지정된 크기의 작은 리스트로 나누어 묶는다.
올림하는 math.ceil()를 이용

from math import ceil

def chunk(lst, size):
    return list(map(
                   lambda x: lst[x * size:x * size + size],
                   # 뒤에서 받은 List를 대입하여 계산한다
                   # ex_1) lst[0*3:0*3+3] = lst[0:3] -> list index 0부터 2까지
                   # ex_2) lst[1*3:1*3+3] = lst[3:6] -> list index 3부터 5까지
                   list(range(0, ceil(len(lst) / size)))
                   # 받은 lst 길이와 지정된 크기 size를 나누어 올림한 값 = n 이라면,
                   # 0부터 n까지의 리스트를 생성한다.
           ))

if __name__ == '__main__':
    print(chunk([3, 2, 3, 41, 3, 4, 8, 3, 4, 5], 3))
# 결과
>>> python chunk.py
[[3, 2, 3], [41, 3, 4], [8, 3, 4], [5]]

출처 :
1. https://30secondsofknowledge.com/
2. https://docs.python.org/3/library/math.html?highlight=ceil#math.ceil

profile
Python이 하고싶은데 자꾸 Flutter 시켜서 빡쳐서 만든 블로그

0개의 댓글