如何在python中按n个元素对元素进行分组

发布于 2024-10-17 07:15:00 字数 132 浏览 2 评论 0原文

给定一个列表 [1,2,3,4,5,6,7,8,9,10,11,12] 和指定的块大小(比如 3),我怎样才能得到一个块列表[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

Given a list [1,2,3,4,5,6,7,8,9,10,11,12] and a specified chunk size (say 3), how can I get a list of chunks [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慈悲佛祖 2024-10-24 07:15:00

好吧,强力答案是:

subList = [theList[n:n+N] for n in range(0, len(theList), N)]

其中 N 是组大小(在您的例子中为 3):

>>> theList = list(range(10))
>>> N = 3
>>> subList = [theList[n:n+N] for n in range(0, len(theList), N)]
>>> subList
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

如果您想要一个填充值,您可以在列表理解之前执行此操作:

tempList = theList + [fill] * N
subList = [tempList[n:n+N] for n in range(0, len(theList), N)]

示例:

>>> fill = 99
>>> tempList = theList + [fill] * N
>>> subList = [tempList[n:n+N] for n in range(0, len(theList), N)]
>>> subList
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 99, 99]]

Well, the brute force answer is:

subList = [theList[n:n+N] for n in range(0, len(theList), N)]

where N is the group size (3 in your case):

>>> theList = list(range(10))
>>> N = 3
>>> subList = [theList[n:n+N] for n in range(0, len(theList), N)]
>>> subList
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

If you want a fill value, you can do this right before the list comprehension:

tempList = theList + [fill] * N
subList = [tempList[n:n+N] for n in range(0, len(theList), N)]

Example:

>>> fill = 99
>>> tempList = theList + [fill] * N
>>> subList = [tempList[n:n+N] for n in range(0, len(theList), N)]
>>> subList
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 99, 99]]
日裸衫吸 2024-10-24 07:15:00

您可以使用 itertools 文档中 recipes 中的 grouper 函数:

from itertools import zip_longest

def grouper(iterable, n, fillvalue=None):
    """Collect data into fixed-length chunks or blocks.

    >>> grouper('ABCDEFG', 3, 'x')
    ['ABC', 'DEF', 'Gxx']
    """
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

You can use the grouper function from the recipes in the itertools documentation:

from itertools import zip_longest

def grouper(iterable, n, fillvalue=None):
    """Collect data into fixed-length chunks or blocks.

    >>> grouper('ABCDEFG', 3, 'x')
    ['ABC', 'DEF', 'Gxx']
    """
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)
我ぃ本無心為│何有愛 2024-10-24 07:15:00

怎么样

a = range(1,10)
n = 3
out = [a[k:k+n] for k in range(0, len(a), n)]

How about

a = range(1,10)
n = 3
out = [a[k:k+n] for k in range(0, len(a), n)]
七月上 2024-10-24 07:15:00

请参阅 itertools 文档底部的示例: http://docs .python.org/library/itertools.html?highlight=itertools#module-itertools

您需要“grouper”方法或类似的方法。

See examples at the bottom of the itertools docs: http://docs.python.org/library/itertools.html?highlight=itertools#module-itertools

You want the "grouper" method, or something like it.

多情癖 2024-10-24 07:15:00
answer = [L[3*i:(3*i)+3] for i in range((len(L)/3) +1)]
if not answer[-1]:
    answer = answer[:-1]
answer = [L[3*i:(3*i)+3] for i in range((len(L)/3) +1)]
if not answer[-1]:
    answer = answer[:-1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文