将列表拆分为n个组 [英] splitting a list into n groups

查看:66
本文介绍了将列表拆分为n个组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否有一种有效的(pythonic)方式,我可以将列表分成

比如5组?通过拆分我的意思是第一个x成员将是一个组,

下一个x成员另一个组,依此类推5次。 (显然x =

lengthof list / 5)


我通过一个简单的for循环并在列表中使用索引来完成这个。

但它看起来并不优雅


谢谢,

Hi,
is there an efficient (pythonic) way in which I could split a list into
say 5 groups? By split I mean the the first x members would be one group,
the next x members another group and so on 5 times. (Obviously x =
lengthof list/5)

I have done this by a simple for loop and using indexes into the list.
But it does''nt seemm very elegant

Thanks,

推荐答案

Rajarshi Guha写道:
Rajarshi Guha wrote:

是否有一种有效的(pythonic)方式,我可以将列表分成
说5组?通过拆分我的意思是第一个x成员将是一个组,
下一个x成员另一个组,依此类推5次。 (显然x =
lengthof list / 5)

我通过一个简单的for循环并使用索引进入列表来完成这个。
但它看起来并不优雅
Hi,
is there an efficient (pythonic) way in which I could split a list into
say 5 groups? By split I mean the the first x members would be one group,
the next x members another group and so on 5 times. (Obviously x =
lengthof list/5)

I have done this by a simple for loop and using indexes into the list.
But it does''nt seemm very elegant




根据你在做什么,这可能适合也可能不合适,但是你可能想看看数字(b $ b)或者它是最终的继承者numarray)。

在这种情况下,你可以使用reshape将你的列表映射到一个x

数组,你可以像对待一样关于索引的嵌套列表。



Depending on what you''re doing, this may or may not be appropriate, but
you might want to look at Numeric (or it''s eventual successor numarray).
In this case, you could use reshape to map your list into a n by x
array, which you can treat like a nested list with respect to indexing.

import Numeric
data = range(25) split = Numeric.reshape(data,(5,5))
拆分
数组([[0,1,2,3,4],

[5, 6,7,8,9],

[10,11,12,13,14],

[15,16,17,18,19],

[20,21,22,23,24]])split [0]
数组([0,1,2,3,4])列表(split [4])
import Numeric
data = range(25)
split = Numeric.reshape(data, (5,5))
split array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]) split[0] array([0, 1, 2, 3, 4]) list(split[4])



[20,21,22 ,23,24]

你有时可以将Numeric与非数字数据一起使用,但它往往是
quircky并且通常不值得麻烦,但如果你已经得到数字

数据,试一试。


-tim


[20, 21, 22, 23, 24]
You can sometimes use Numeric with nonnumeric data, but it tends to be
quircky and often is not worth the trouble, but if you''ve got numeric
data, try it out.

-tim


Rajarshi Guha< ra ****** @ presidency.com>写道:
Rajarshi Guha <ra******@presidency.com> writes:

是否有一种有效的(pythonic)方式,我可以将列表分成
说5组?通过拆分我的意思是第一个x成员将是一个组,
下一个x成员另一个组,依此类推5次。 (显然x =
lengthof list / 5)
Hi,
is there an efficient (pythonic) way in which I could split a list into
say 5 groups? By split I mean the the first x members would be one group,
the next x members another group and so on 5 times. (Obviously x =
lengthof list/5)




怎么样


------------------------------------------------- -----------

def span(x,n):

返回范围(0,x,n)


def group(l,num):

n =(len(l)/ num)+ 1

return [l [s:s + n对于s in span(len(l),n)]


#测试用例

l1 =范围(100)

打印组(l1,5)

打印组(l1,6)

打印组(l1,4)

---- -------------------------------------------------- ------


即使跨度可以缩小以缩短它,抽象出来

允许你命名并记录它的作用(返回每个

SPAN的n项的起始索引)。此外,它并不担心最后一个列表与其余列表的大小相同。我懒得检查边界条件

(读者练习)。一个完全没有懒惰的人可能会定义更原始的group_by_n:


def group_by_n(l,n):

返回[l [s:s + n]为s in span(len(l),n)]


并根据其定义group_into_num_pieces:


def group_into_num_pieces(l,num):

返回group_by_n(l,(len(l)/ num)+ 1)


给你3个有用的功能,价格为1,但我太懒了。


Eddie



How about:

------------------------------------------------------------
def span (x, n):
return range(0, x, n)

def group (l, num):
n = (len(l)/num) + 1
return [l[s:s+n] for s in span (len(l), n)]

# test cases
l1 = range(100)
print group (l1, 5)
print group (l1, 6)
print group (l1, 4)
------------------------------------------------------------

Even though span could be folded in to make it shorter, abstracting it out
allows you to name and document what it does (return the start index of each
SPAN of n items). Also it doesn''t worry too much about the last list being
the same size as the rest. I''m too lazy to check the boundary conditions
properly (an exercise for the reader). A completely non lazy person would
possibly define the more primitive group_by_n:

def group_by_n (l, n):
return [l[s:s+n] for s in span (len(l), n)]

and define group_into_num_pieces in terms of that:

def group_into_num_pieces (l, num):
return group_by_n (l,(len(l)/num) + 1)

giving you 3 useful functions for the price of 1, but I''m too lazy for that.

Eddie




" Rajarshi Guha" < RA ****** @ presidency.com>在留言中写道

news:pa ********************************** @ presiden cy.com ...

"Rajarshi Guha" <ra******@presidency.com> wrote in message
news:pa**********************************@presiden cy.com...

是否有一种有效的(pythonic)方式,我可以将列表
分成5组?通过拆分我的意思是前x个成员将是一个
组,下一个x成员是另一个组,依此类推5次。 (显然x =
lengthof list / 5)

我通过一个简单的for循环并使用索引到
列表中完成了这个。但它看起来并不优雅
Hi,
is there an efficient (pythonic) way in which I could split a list into say 5 groups? By split I mean the the first x members would be one group, the next x members another group and so on 5 times. (Obviously x =
lengthof list/5)

I have done this by a simple for loop and using indexes into the list. But it does''nt seemm very elegant




它是否适用于所有输入案例?它可读吗?它是否可以快速地接受
?如果是这样,那可能就是''Pythonic''。


TJR



Does it work correctly for all input cases? Is it readable? Is it
acceptibly fast? If so, it is probably ''Pythonic'' enough.

TJR


这篇关于将列表拆分为n个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆