python - 使用另一个列表中一致的索引范围创建新列表

标签 python ipython

寻求帮助,请从 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/

相关文章:

python - 在 Pycharm Ubuntu 中添加 ipython 作为解释器

python - 尽管有 Nat 网关,但使用 Python 的 Lambda 函数请求仍超时

Python - 使用冗长的正则表达式解析用户输入

python - reactor.iterate 似乎用 Py2exe 阻止了程序

python - django + 应用程序引擎中的仅限管理区域

python - 覆盖 ipython 退出函数 - 或者在其中添加 Hook

python - 在 iPython qtconsole 中显示图像

python - 将相同的 numpy 数组与标量相乘多次

ipython - 更改代码时,ipython Notebook不更新

python - 如何在谷歌云数据实验室的另一个笔记本中执行Python笔记本