创建子列表
与列表扁平化相反。
给定一个列表和长度 n,返回长度为 n 的子列表的列表。
def sublist(lst, n):
sub=[] ; result=[]
for i in lst:
sub+=[i]
if len(sub)==n: result+=[sub] ; sub=[]
if sub: result+=[sub]
return result
举个例子:
如果列表是:
[1,2,3,4,5,6,7,8]
并且 n 是:
3
返回:
[[1, 2, 3], [4, 5, 6], [7, 8]]
有没有更雄辩/简洁的方式?
顺便说一句,将列表附加到列表时首选什么(在上面的上下文中):
list1+=[list2]
或者:
list1.append(list2)
考虑到(根据 Summerfeild 的“Python 3 编程”)它们是相同的?
谢谢。
The opposite of list flattening.
Given a list and a length n return a list of sub lists of length n.
def sublist(lst, n):
sub=[] ; result=[]
for i in lst:
sub+=[i]
if len(sub)==n: result+=[sub] ; sub=[]
if sub: result+=[sub]
return result
An example:
If the list is:
[1,2,3,4,5,6,7,8]
And n is:
3
Return:
[[1, 2, 3], [4, 5, 6], [7, 8]]
Is there a more eloquent / concise way?
An aside, what is preferred when appending lists to lists (in the context above):
list1+=[list2]
Or:
list1.append(list2)
Given that (according to Summerfeild's 'Programming in Python 3') they are the same?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这样的列表列表可以使用 列表理解 构建:
有还有 石斑鱼习语:
但请注意,缺少的元素将用值 None 填充。 izip_longest 也可以采用
fillvalue
参数如果需要除“无”之外的其他内容。list1+=[list2]
——注意这次的括号——相当于list1.append(list2)
。我写代码时最优先考虑的是可读性,不是速度。因此,我会选择
list1.append(list2)
。然而,可读性是主观的,并且可能很大程度上受到您所熟悉的习语的影响。令人高兴的是,在这种情况下,可读性和速度似乎是一致的:
Such a list of lists could be constructed using a list comprehension:
There is also the grouper idiom:
but note that missing elements are filled with the value None. izip_longest can take a
fillvalue
parameter as well if something other than None is desired.list1+=[list2]
-- noting the brackets this time -- is equivalent tolist1.append(list2)
. My highest priority when writing code is readability,not speed. For this reason, I would go with
list1.append(list2)
. Readability is subjective, however, and probably is influenced greatly by what idioms you're familiar with.Happily, in this case, readability and speed seem to coincide:
下面的内容怎么样(其中
x
是您的列表):对于
n!=3
来说,这很简单。至于你的第二个问题,它们是等效的,所以我认为这是风格问题。但是,请确保您没有令人困惑<代码>附加和<代码>扩展。
How about the following (where
x
is your list):This is trivial to generalize for
n!=3
.As to your second question, they're equivalent so I think it's a matter of style. However, do make sure you're not confusing
append
withextend
.您听说过
boltons
吗?它具有您想要的内置功能,称为
分块
输出:
并且
boltons
中更有吸引力的是它有分块
作为迭代器,称为chunked_iter
,因此您不需要存储整个事情都在记忆中。很整洁,对吧?Have you heard of
boltons
?It has what you want, built-in, called
chunked
Output:
And whats more attractive in
boltons
is that it haschunked
as an iterator, calledchunked_iter
, so you don't need to store the whole thing in memory. Neat, right?该函数可以采用任何类型的可迭代(不仅是已知长度的序列):
This function can take any kind of iterable (not only sequences of known length):
我认为这个 split 函数可以满足您的需求(尽管它适用于任何迭代器而不仅仅是列表):
编辑:关于您的旁白,我总是使用 list.append(blah),因为它对我来说更惯用,但是我相信它们在功能上是等效的。
I think this split function does what you're looking for (though it works with any iterator rather than just lists):
Edit: Regarding your asside, I always use list.append(blah), as it feels more idiomatic to me, but I believe they are functionally equivalent.
对于某些特定情况,使用 numpy 包可能会很有用。在此包中,您有一个 reshape 例程:
但是,如果它不是 n 的倍数,则此解决方案不会填充您的列表。
For some specific cases, it might be useful to use the numpy package. In this package you have a reshape routine:
However, this solution won't pad your list, if it's not a multiply of n.
我知道,它看起来像 brainfuck,但它是有效的:
I know, it looks like a brainfuck, but is works: