Python split list into n chunks

前端 未结 17 1443
情深已故
情深已故 2020-12-02 13:42

I know this question has been covered many times but my requirement is different.

I have a list like: range(1, 26). I want to divide this list into a fi

17条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 13:45

    If you want to have the chunks as evenly sized as possible:

    def chunk_ranges(items: int, chunks: int) -> List[Tuple[int, int]]:
        """
        Split the items by best effort into equally-sized chunks.
        
        If there are fewer items than chunks, each chunk contains an item and 
        there are fewer returned chunk indices than the argument `chunks`.
    
        :param items: number of items in the batch.
        :param chunks: number of chunks
        :return: list of (chunk begin inclusive, chunk end exclusive)
        """
        assert chunks > 0, \
            "Unexpected non-positive chunk count: {}".format(chunks)
    
        result = []  # type: List[Tuple[int, int]]
        if items <= chunks:
            for i in range(0, items):
                result.append((i, i + 1))
            return result
    
        chunk_size, extras = divmod(items, chunks)
    
        start = 0
        for i in range(0, chunks):
            if i < extras:
                end = start + chunk_size + 1
            else:
                end = start + chunk_size
    
            result.append((start, end))
            start = end
    
        return result
    

    Test case:

    def test_chunk_ranges(self):
        self.assertListEqual(chunk_ranges(items=8, chunks=1),
                             [(0, 8)])
    
        self.assertListEqual(chunk_ranges(items=8, chunks=2),
                             [(0, 4), (4, 8)])
    
        self.assertListEqual(chunk_ranges(items=8, chunks=3),
                             [(0, 3), (3, 6), (6, 8)])
    
        self.assertListEqual(chunk_ranges(items=8, chunks=5),
                             [(0, 2), (2, 4), (4, 6), (6, 7), (7, 8)])
    
        self.assertListEqual(chunk_ranges(items=8, chunks=6),
                             [(0, 2), (2, 4), (4, 5), (5, 6), (6, 7), (7, 8)])
    
        self.assertListEqual(
            chunk_ranges(items=8, chunks=7),
            [(0, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)])
    
        self.assertListEqual(
            chunk_ranges(items=8, chunks=9),
            [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)])
    

提交回复
热议问题