You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python技术问询:如何按每n个元素生成嵌套列表?含每5个元素拆分需求

拆分列表为每n个元素的嵌套列表(附Python按5个元素拆分的实现)

Hey there! Let's break down how to split a list into nested chunks of n elements, plus a concrete example for splitting into chunks of 5 in Python.

一、通用方案:按任意n个元素拆分列表

方法1:切片 + 列表推导式(最简洁)

This is the go-to method for most cases—it's concise and easy to read. We use Python's slicing feature to grab chunks of n elements at a time.

def split_list(lst, n):
    # 生成起始索引,步长为n,然后每次取n个元素的切片
    return [lst[i:i+n] for i in range(0, len(lst), n)]

If your list isn't perfectly divisible by n, the last chunk will just have the remaining elements—no extra work needed!

方法2:使用itertools.islice(适合大型迭代器)

If you're working with a huge iterable (not just a list) and want to avoid loading everything into memory at once, itertools.islice is your friend. It processes elements on the fly:

from itertools import islice

def split_iterable(iterable, n):
    it = iter(iterable)
    while True:
        # 每次从迭代器中取n个元素
        chunk = list(islice(it, n))
        if not chunk:  # 没有元素了就退出循环
            break
        yield chunk

To get a list result, just wrap the function call in list(): list(split_iterable(your_list, n))

方法3:手动循环(适合理解底层逻辑)

If you're new to Python and want to see exactly how the splitting works, a manual loop makes the process crystal clear:

def split_list_manual(lst, n):
    result = []
    current_chunk = []
    for item in lst:
        current_chunk.append(item)
        # 当当前chunk凑够n个元素时,加入结果并重置
        if len(current_chunk) == n:
            result.append(current_chunk)
            current_chunk = []
    # 处理最后剩下的不足n个的元素
    if current_chunk:
        result.append(current_chunk)
    return result

二、Python中按每5个元素拆分列表的实现及示例

Let's use a sample list to show how these methods work with n=5:

示例场景

Suppose we have this original list:

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

实现1:列表推导式

n = 5
split_result = [original_list[i:i+n] for i in range(0, len(original_list), n)]
print(split_result)

输出示例:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]

实现2:itertools.islice

from itertools import islice

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
n = 5

it = iter(original_list)
split_result = []
while True:
    chunk = list(islice(it, n))
    if not chunk:
        break
    split_result.append(chunk)

print(split_result)

输出示例:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]

实现3:手动循环

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
n = 5
result = []
current_chunk = []

for num in original_list:
    current_chunk.append(num)
    if len(current_chunk) == n:
        result.append(current_chunk)
        current_chunk = []
# 别忘了最后剩下的元素
if current_chunk:
    result.append(current_chunk)

print(result)

输出示例:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]

内容的提问来源于stack exchange,提问作者Hannan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:54:16