更好的方法来编写这个功能 [英] better way to write this function
问题描述
您好,
此功能可以满足我的需求。但我想知道是否有更简单/更好的方式。说实话,我不太了解
什么是pythonic意味着。
def divide_list(lst,n):
"""将列表分成多个列表,每个列表包含n个项目。额外的
项目是
被忽略,如果有的话。""
cnt = len(lst)/ n
rv = [[范围(n)中的i无效] i范围内(cnt)]
for i in range(cnt):
for j在范围(n):
rv [i] [j] = lst [i * n + j]
返回rv
谢谢!
Kelie写道:
你好,
这个功能可以满足我的需要。但我想知道是否有更简单/更好的方式。说实话,我不太了解
什么是pythonic意味着。
def divide_list(lst,n):
"""将列表分成多个列表,每个列表包含n个项目。额外的
项目是
被忽略,如果有的话。""
cnt = len(lst)/ n
rv = [[范围(n)中的i无效] i范围内(cnt)]
for i in range(cnt):
for j在范围(n)中:
rv [i] [j] = lst [i * n + j]
返回rv
您可以使用切片:
>> def chunks (items,n):
....在范围内返回[items [start:start + n] n(0 ,len(items)-n + 1,n)]
....
>> for i in range(1,10):
.... print块(范围(5),i)
....
[[0],[1],[2],[3],[4]]
[[0,1],[2,3]]
[[0,1,2]]
[[0,1,2,3]]
[[0,1,2,3, 4]]
[]
[]
[]
[]
或构建一个适用于任意迭代的生成器:
>> ;来自itertools import *
def chunks(items,n):
.... items = iter(items )
....而1:
.... chunk = list(islice(items,n-1))
。 ... chunk.append(items.next())
.... yield chunk
....
>> list(chunk(range(5),2))
[[0,1],[2,3]]
彼得
< blockquote> 11月26日上午9:42,Kelie< kf9 ... @ gmail.comwrot e:
您好,
此功能可以满足我的需求。但我想知道是否有更简单/更好的方式。说实话,我不太了解
什么是pythonic意味着。
def divide_list(lst,n):
"""将列表分成多个列表,每个列表包含n个项目。额外的
项目是
被忽略,如果有的话。""
cnt = len(lst)/ n
rv = [[范围(n)中的i无效] i范围内(cnt)]
for i in range(cnt):
for j在范围(n):
rv [i] [j] = lst [i * n + j]
返回rv
谢谢!
x = [''1'',''2'',''3'',''4'',''5'','' 6'',''7'',''8'']
def divide_list(lst,n):
rv = []
为范围内的i(int(round((len(lst)/ n),0))):
rv.append(lst [i * n:(i + 1)* n ])
返回rv
tmp = divide_list(x,3)
tmp
[ [''''','''',''3''],[''''',''''',''''''''' $ b一种方法。
Chris< cw **** @ gmail.comwrites:
$ b我在范围内的$ b(int(round((len(lst)/ n),0))):...
呃!!!甚至不正确(在未来的分工下),除了丑陋之外。
我认为你的意思是:
$ x $ b for x in xrange(len(lst)// n ):...
实际上,这个分组函数经常被重新实现,它应该被构建到stdlib中,可能是在itertools中。
Hello,
This function does I what I want. But I''m wondering if there is an
easier/better way. To be honest, I don''t have a good understanding of
what "pythonic" means yet.
def divide_list(lst, n):
"""Divide a list into a number of lists, each with n items. Extra
items are
ignored, if any."""
cnt = len(lst) / n
rv = [[None for i in range(n)] for i in range(cnt)]
for i in range(cnt):
for j in range(n):
rv[i][j] = lst[i * n + j]
return rv
Thanks!
Kelie wrote:
Hello,
This function does I what I want. But I''m wondering if there is an
easier/better way. To be honest, I don''t have a good understanding of
what "pythonic" means yet.
def divide_list(lst, n):
"""Divide a list into a number of lists, each with n items. Extra
items are
ignored, if any."""
cnt = len(lst) / n
rv = [[None for i in range(n)] for i in range(cnt)]
for i in range(cnt):
for j in range(n):
rv[i][j] = lst[i * n + j]
return rvYou can use slicing:
>>def chunks(items, n):
.... return [items[start:start+n] for n in range(0, len(items)-n+1, n)]
....>>for i in range(1,10):
.... print chunks(range(5), i)
....
[[0], [1], [2], [3], [4]]
[[0, 1], [2, 3]]
[[0, 1, 2]]
[[0, 1, 2, 3]]
[[0, 1, 2, 3, 4]]
[]
[]
[]
[]
Or build a generator that works with arbitrary iterables:
>>from itertools import *
def chunks(items, n):
.... items = iter(items)
.... while 1:
.... chunk = list(islice(items, n-1))
.... chunk.append(items.next())
.... yield chunk
....>>list(chunks(range(5), 2))
[[0, 1], [2, 3]]
Peter
On Nov 26, 9:42 am, Kelie <kf9...@gmail.comwrote:Hello,
This function does I what I want. But I''m wondering if there is an
easier/better way. To be honest, I don''t have a good understanding of
what "pythonic" means yet.
def divide_list(lst, n):
"""Divide a list into a number of lists, each with n items. Extra
items are
ignored, if any."""
cnt = len(lst) / n
rv = [[None for i in range(n)] for i in range(cnt)]
for i in range(cnt):
for j in range(n):
rv[i][j] = lst[i * n + j]
return rv
Thanks!x = [''1'', ''2'', ''3'', ''4'', ''5'', ''6'', ''7'', ''8'']
def divide_list(lst, n):
rv = []
for i in range(int(round((len(lst)/n),0))):
rv.append(lst[i*n:(i+1)*n])
return rv
tmp = divide_list(x, 3)
tmp
[[''1'', ''2'', ''3''], [''4'', ''5'', ''6'']]
One way to do it.
Chris <cw****@gmail.comwrites:for i in range(int(round((len(lst)/n),0))): ...Ugh!!! Not even correct (under future division), besides being ugly.
I think you mean:
for i in xrange(len(lst) // n): ...
Really though, this grouping function gets reimplemented so often that
it should be built into the stdlib, maybe in itertools.
这篇关于更好的方法来编写这个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!