THUMBS SHIFT→

このブログは主に親指シフトを用いて書かれています

pythonでリストをn要素の配列に分割する

def divide_list(list_, n):
    """
    divide list into sub-list which have n elements.Reminder is appended to it.
    Args:
        list_ (list): list to divide
        n (int): number of elements which sublist has

    Returns:
        list: divided list.It's length is len(list_) // 30 + (1 or 0)

    Examples:
        >>> divide_list(list(range(11)), 3)
        [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
    """
    divided_list = [list_[i:i + n] for i in range(0, len(list_), n)]
    return divided_list