使用来自另一个列表的一致索引范围创建新列表
Creating new lists with a consistent range of indexes from another list
寻求帮助,请从 100 多个项目的列表中创建新列表。
我能够复制列表并根据间隔创建新列表,但我想根据各个索引的有序范围创建新列表。
所以我在 iPython 'whos':
Variable / List / n=105 [0:105] items...
我想创建:
list a --> that has items in the above[0:7]
list b --> that has items in the above[7:14]
list c --> that has items in the above[14:21]
所有 105 个项目的范围一致。
我想知道是否有内置模块或第 3 方(集合?)可以执行此操作。或者也许是 @Generator 表达式...我不确定。
[items[k:k+7] for k in range(0,105,7)]
一般来说,
[items[k:k+m] for k in range(0,len(items),m)]
将列表item拆分为固定长度的子列表m
寻求帮助,请从 100 多个项目的列表中创建新列表。 我能够复制列表并根据间隔创建新列表,但我想根据各个索引的有序范围创建新列表。 所以我在 iPython 'whos':
Variable / List / n=105 [0:105] items...
我想创建:
list a --> that has items in the above[0:7]
list b --> that has items in the above[7:14]
list c --> that has items in the above[14:21]
所有 105 个项目的范围一致。
我想知道是否有内置模块或第 3 方(集合?)可以执行此操作。或者也许是 @Generator 表达式...我不确定。
[items[k:k+7] for k in range(0,105,7)]
一般来说,
[items[k:k+m] for k in range(0,len(items),m)]
将列表item拆分为固定长度的子列表m