寻求帮助,请从 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 项的范围都是一致的。
我想知道是否有内置模块或第三方(Collections?)可以做到这一点。或者也许是 @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的子列表
关于python - 使用另一个列表中一致的索引范围创建新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32431404/