简单列表划分问题 [英] Simple List division problem
问题描述
如何将列表分成一组子列表 - 如果列表是
不能均匀分割?
考虑这个例子:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3#我要打破的列表数x进入
z = y / x
我想得到的是3个子列表
print z [0] = [1 ,2,3]
print z [2] = [4,5,6]
print z [3] = [7,8,9,10]
显然不是偶数,一个列表将有4个元素,另外2个将b / b
具有3.,
最重要的逻辑,就是我会获得3个列表并找到一种方法来使用
python来尝试均匀地打破它,如果没有一个列表可以有更大的元素
我会使用itertools吗?我该怎么做?
谢谢
marcstuart写道:
如果列表是
不能均匀分割,我如何将列表分成一组子列表?
考虑这个例子:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3#number of列表我想打破x进入
z = y / x
我想得到的是3个子列表
print z [0] = [1,2,3]
print z [2] = [4,5,6]
print z [3] = [7,8,9,10]
显然不是偶数,一个列表将有4个元素,另外2个将b / b
。最重要的逻辑是,我将获得3个列表,并找到一种方法来使用
python尝试均匀地破解它,如果不是一个列表可以有更大的
元素数量
我会使用itertools吗?我该怎么做?
谢谢
计算正常子列表的大小,以及额外的
与最后一个子列表一致。
然后提取y-1正常大小的子列表和一个可能更大的子列表。
>> x = [1,2,3,4,5,6,7,8,9,10]
y = 3
s = len(x)/ y
s#正常子列表的大小
3
个;> E = LEN(X) - Ÿ* S#为最后子列表多余的元素
E
'/ BLOCKQUOTE>
1个
>> z = [x [s * i:s * i + s] for i in range(y-1)] + [x [-se:]] #extract y-1
正常+ 1个较大的子列表
>> z
[[1,2,3],[4,5,6] ,[7,8,9,10]]
完成!
加里赫伦
marcstuart写道:
如果列表是
不能均匀分割?考虑这个例子:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3#number列表我想打破x进入
z = y / x
我想得到的是3个子列表
打印z [0] = [1,2,3]
打印z [2] = [4,5,6]
打印z [3] = [7,8,9,10]
显然不是偶数,一个列表将有4个元素,另外2个将
有3.,
这里有一种方法:
#砍掉它
n = len(x)/ y
z = [x [i:i + n] for x in xrange(0,len(x),n)]
#如果最后一块太短,请将它添加到它之前
如果len(z [-1])< n和len(z)1:
z [-2] .extend(z.pop(-1))
< / F>
1月12日,12:37 * pm,marcstuart< marc.stuart.ris ... @ gmail.comwrote:
如果列表是
不能均匀分割,我如何将列表分成一组子列表?
考虑这个例子:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3 * * * #我要打破x的列表数量
z = y / x
我想得到的是3个子列表
>
print z [0] = [1,2,3]
print z [2] = [4,5,6]
print z [ 3] = [7,8,9,10]
显然不是偶数,一个列表将有4个元素,另外2个将b / b
有3个。 ,
最重要的逻辑,就是我将得到3个列表并找到一种方法来使用
python来尝试均匀地破坏它,如果不是一个列表可以有更大的
元素数量
我会使用iterto吗? ols?我该怎么做?
谢谢
def list_split(x,y):
dm = divmod(len(x),y)
如果dm [1]!= 0:
z = [x [i * y:i * y + y ] for x in xrange(len(x)/ y)if len(x [i * y:])> = 2 * y]
z.append(x [(len(x) / y-1)* y:])
else:
z = [x [i * y:i * y + y] for x in xrange(len(x) )/ Y)]
返回ž
个;> ; list_split([1,2,3,4,5,6,7,8,9,10],3)
[[1,2,3],[4,5,6],[7,8,9,10]]
>> list_split([1,2,3,4,5,6,7,8,9,10],5)
[[1,2,3,4,5],[6,7,8,9,10]]
>&g t; list_split([1,2,3,4,5,6,7,8,9,10],4)
[[1,2,3,4],[5,6,7,8,9,10]]
>> list_split([1,2,3,4,5,6,7,8,9,10],2)
[[1,2],[3,4],[5,6],[7,8],[9,10]]
How do I divide a list into a set group of sublist''s- if the list is
not evenly dividable ?
consider this example:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3 # number of lists I want to break x into
z = y/x
what I would like to get is 3 sublists
print z[0] = [1,2,3]
print z[2] = [4,5,6]
print z[3] = [7,8,9,10]
obviously not even, one list will have 4 elements, the other 2 will
have 3.,
the overriding logic, is that I will get 3 lists and find a way for
python to try to break it evenly, if not one list can have a greater
amount of elements
Would I use itertools ? How would I do this ?
Thanks解决方案marcstuart wrote:How do I divide a list into a set group of sublist''s- if the list is
not evenly dividable ?
consider this example:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3 # number of lists I want to break x into
z = y/x
what I would like to get is 3 sublists
print z[0] = [1,2,3]
print z[2] = [4,5,6]
print z[3] = [7,8,9,10]
obviously not even, one list will have 4 elements, the other 2 will
have 3.,
the overriding logic, is that I will get 3 lists and find a way for
python to try to break it evenly, if not one list can have a greater
amount of elements
Would I use itertools ? How would I do this ?
Thanks
Calculate the size of a normal sublist, and the amount of extra that
goes with the last sublist.
Then extract y-1 normal sized sublists and one possibly larger sublist.
>>x = [1,2,3,4,5,6,7,8,9,10]
y = 3
s = len(x)/y
s # size of normal sublists
3
>>e = len(x) - y*s # extra elements for last sublist
e
1
>>z = [x[s*i:s*i+s] for i in range(y-1)] + [x[-s-e:]] #extract y-1
normal + 1 larger sublists
>>z
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
Done!
Gary Herron
marcstuart wrote:
How do I divide a list into a set group of sublist''s- if the list is
not evenly dividable ? consider this example:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3 # number of lists I want to break x into
z = y/x
what I would like to get is 3 sublists
print z[0] = [1,2,3]
print z[2] = [4,5,6]
print z[3] = [7,8,9,10]
obviously not even, one list will have 4 elements, the other 2 will
have 3.,here''s one way to do it:
# chop it up
n = len(x) / y
z = [x[i:i+n] for i in xrange(0, len(x), n)]
# if the last piece is too short, add it to one before it
if len(z[-1]) < n and len(z) 1:
z[-2].extend(z.pop(-1))
</F>
On Jan 12, 12:37*pm, marcstuart <marc.stuart.ris...@gmail.comwrote:How do I divide a list into a set group of sublist''s- if the list is
not evenly dividable ?
consider this example:
x = [1,2,3,4,5,6,7,8,9,10]
y = 3 * * *# number of lists I want to break x into
z = y/x
what I would like to get is 3 sublists
print z[0] = [1,2,3]
print z[2] = [4,5,6]
print z[3] = [7,8,9,10]
obviously not even, one list will have 4 elements, the other 2 will
have 3.,
the overriding logic, is that I will get 3 lists and find a way for
python to try to break it evenly, if not one list can have a greater
amount of elements
Would I use itertools ? How would I do this ?
Thanksdef list_split(x,y):
dm = divmod(len(x),y)
if dm[1] != 0:
z = [x[i*y:i*y+y] for i in xrange(len(x)/y) if len(x[i*y:])>=2*y]
z.append(x[(len(x)/y-1)*y:])
else:
z = [x[i*y:i*y+y] for i in xrange(len(x)/y)]
return z
>>list_split([1,2,3,4,5,6,7,8,9,10],3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
>>list_split([1,2,3,4,5,6,7,8,9,10],5)
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
>>list_split([1,2,3,4,5,6,7,8,9,10],4)
[[1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
>>list_split([1,2,3,4,5,6,7,8,9,10],2)
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
这篇关于简单列表划分问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!