如何用n个元素在python中对元素进行分组? [重复]

时间:2021-10-14 21:40:57

Possible Duplicate:
How do you split a list into evenly sized chunks in Python?

可能重复:如何在Python中将列表拆分为大小均匀的块?

I'd like to get groups of size n elements from a list l:

我想从列表l中获取大小为n的元素组:

ie:

即:

[1,2,3,4,5,6,7,8,9] -> [[1,2,3], [4,5,6],[7,8,9]] where n is 3

5 个解决方案

#1


19  

You can use grouper from the recipes on the itertools documentation page:

您可以在itertools文档页面上使用食谱中的grouper:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

#2


14  

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):

其中N是组大小(在您的情况下为3):

>>> theList = 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]]

#3


3  

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

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

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

你想要“石斑鱼”方法,或类似的东西。

#4


1  

How about

怎么样

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

#5


0  

answer = [L[3*i:(3*i)+3] for i in range((len(L)/3) +1)]
if not answer[-1]:
    answer = answer[:-1]

#1


19  

You can use grouper from the recipes on the itertools documentation page:

您可以在itertools文档页面上使用食谱中的grouper:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

#2


14  

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):

其中N是组大小(在您的情况下为3):

>>> theList = 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]]

#3


3  

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

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

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

你想要“石斑鱼”方法,或类似的东西。

#4


1  

How about

怎么样

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

#5


0  

answer = [L[3*i:(3*i)+3] for i in range((len(L)/3) +1)]
if not answer[-1]:
    answer = answer[:-1]