将列表拆分为另一个列表指定的大小 [英] Spliting a list into sizes specified by another list

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

问题描述

假设有一个列表X和另一个列表 num_items ,它指定了子列表中应该包含的项目数,我可以手动拆分列表:

Let say there's a list X and another list num_items that specify the number of items that should be in the sublist, I can split the list manually as such:

>>> x = list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> num_items = [4, 4, 2]

>>> slice1 = x[:num_items[0]]
>>> slice2 = x[len(slice1):len(slice1)+num_items[1]]
>>> slice3 = x[len(slice1)+len(slice2):]
>>> slice1, slice2, slice3
([0, 1, 2, 3], [4, 5, 6, 7], [8, 9])

有两种情况会导致最后几片切片出现问题,例如但是,如果我手动编码3个切片,可以使用空列表解决这个问题:

There will be two cases where the last few slices can become problematic, e.g. but that can be solved with the empty list given that I manually code the 3 slices:

>>> num_items = [9, 1, 1]
>>> slice1 = x[:num_items[0]]
>>> slice2 = x[len(slice1):len(slice1)+num_items[1]]
>>> slice3 = x[len(slice1)+len(slice2):]
>>> slice1, slice2, slice3
([0, 1, 2, 3, 4, 5, 6, 7, 8], [9], [])

如果有4个切片怎么办,例如:

What if there are 4 slices, e.g.:

>>> num_items = [9, 1, 1, 2]
>>> slice1 = x[:num_items[0]]
>>> slice2 = x[len(slice1):len(slice1)+num_items[1]]
>>> slice3 = x[len(slice1)+len(slice2):len(slice1)+len(slice2)+num_items[2]]
>>> slice4 = x[len(slice1)+len(slice2)+len(slice3): len(slice)+len(slice2)+len(slice3)+num_items[3]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'type' has no len()

所需的输出将是将空列表添加到第4个切片,即:

The desired output would be to add empty list to the 4th slice, i.e.:

>>> slice1, slice2, slice3, slice4
([0, 1, 2, 3, 4, 5, 6, 7, 8], [9], [], [])

如果 num_items 需要的项目少于X的长度,只需返回总和即可 num_items ,即

If the num_items requires lesser items than length of X, simply return until the sum of num_items, i.e.

>>> num_items = [4, 4]
>>> slice1, slice2
([0, 1, 2, 3], [4, 5, 6, 7])

主要问题是有没有办法在不手动编码分割的情况下拆分切片?(覆盖 num_items <的情况的答案/ code>请求多于X的项目,在这种情况下,应返回空的子列表)

The main question is is there a way to split up the slices without manually coding the splits? (The answer to cover the case where the num_items requests for more items than in X, in that case, empty sublists should be returned)

请记住,X的长度可以是相当的大(即,>百亿),但项数的范围从1到100 =)

Keep in mind that the length of X can be rather big (i.e. > 10,000,000,000) but the length of num_items ranges from 1 to 100 =)

推荐答案

这里有不同的方法:

[[x.pop(0) for _ in x[:s]] for s in num_items]

示例:

>>> x = range(10)
>>> n = [9, 1, 1]
>>> [[x.pop(0) for y in x[:s]] for s in n]
[[0, 1, 2, 3, 4, 5, 6, 7, 8], [9], []]

>>> x = range(10)
>>> n = [2, 2, 2, 2, 2, 2]
>>> [[x.pop(0) for y in x[:s]] for s in n]
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], []]

>>> x = range(10)
>>> n = [3, 4, 5, 2]
>>> [[x.pop(0) for y in x[:s]] for s in n]
[[0, 1, 2], [3, 4, 5, 6], [7, 8, 9], []] # Notice here how slice 5 only returns 3 numbers because there are only 3 numbers left in x

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

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