Python/기초

list 나눠서 출력

딸기우유중독 2021. 10. 20. 15:57

#10개씩 출력

for i in range(0, len(Cube_Finded), 10):

        print(Cube_Finded[i:i+10])

 

# n개씩 출력

 

def list_chunk(lst, n):
    return [lst[i:i+n] for i in range(0, len(lst), n)]

list_test = list(range(1,32))
print("분할 전 : ", list_test)

list_chunked = list_chunk(list_test, 7)
print("분할 후 : ", list_chunked)

# 출력
# 분할 전 :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
#  후 :  [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31]]

 

출처: https://jsikim1.tistory.com/141

 

Python list chunk - 리스트 분할(자르기, 나누기, split) 방법

Python list chunk - 리스트(배열) 분할(자르기, 나누기, split) 방법 Python에서 list를 원하는 간격으로 나누는 방법을 알려드리도록 하겠습니다. PHP에서는 array_chunk() 를 사용하여 배열을 원하는..

jsikim1.tistory.com

 

728x90