python - 如何将列表与嵌套列表python的长度分开?
问题描述
我有一个这样的嵌套列表:
a = [[0.00069053395], [0.7278625], [0.8591849, 0.86290157, 0.8703022], [0.9041279, 0.9102304, 0.91197634], [0.93096334, 0.93327045], [0.9456768, 0.95339334], [0.98164046, 0.9836741]]
和下面的另一个列表:
b = ['/home/shape/13.jpg', '/home/shape/5.jpg', '/home/shape/6.jpg', '/home/shape/0.jpg', '/home/shape/2.jpg', '/home/shape/1.jpg', '/home/shape/7.jpg', '/home/shape/11.jpg', '/home/cuts/shape/10.jpg', '/home/shape/4.jpg', '/home/shape/14.jpg', '/home/shape/12.jpg', '/home/shape/16.jpg', '/home/shape/8.jpg']
我想让列表b
类似于嵌套列表a
,其元素的长度。
预期输出:
c = [['/home/shape/13.jpg'],['/home/shape/5.jpg'],['/home/shape/6.jpg', '/home/shape/0.jpg', '/home/shape/2.jpg'],['/home/shape/1.jpg', '/home/shape/7.jpg', '/home/shape/11.jpg'],['/home/cuts/shape/10.jpg', '/home/shape/4.jpg'],['/home/shape/14.jpg', '/home/shape/12.jpg'],['/home/shape/16.jpg', '/home/shape/8.jpg']]
任何的意见都将会有帮助!
解决方案
您可以将iter内置函数与列表理解一起使用:
it = iter(b)
[[next(it) for _ in l] for l in a]
输出:
[['/home/shape/13.jpg'],
['/home/shape/5.jpg'],
['/home/shape/6.jpg', '/home/shape/0.jpg', '/home/shape/2.jpg'],
['/home/shape/1.jpg', '/home/shape/7.jpg', '/home/shape/11.jpg'],
['/home/cuts/shape/10.jpg', '/home/shape/4.jpg'],
['/home/shape/14.jpg', '/home/shape/12.jpg'],
['/home/shape/16.jpg', '/home/shape/8.jpg']]
推荐阅读
- python - /bin/sh: 1: 语法错误:单词意外 - 使用 Python 中的 os.system()
- dart - 如何使用颤振编写按下双后退按钮以退出应用程序
- python - 数据框:删除具有某些类型值的对象类型列
- androidx - AndroidX itemTouchListener
- angularjs - 如何让我的自定义 JavaScript 代码在 AngularJS MVC 中正常工作?
- git - Visual Studio 的源代码控制从 TFVC 自动切换到 Git
- gnuplot - 不同缩放 y 轴之间的填充曲线
- java - Android java.io.IOException: write failed: EBADF (Bad file number)
- ignite - “二进制类型具有不同的字段类型”在键或值中使用日期类型字段时出错
- sitecore - 我们如何在 sitecore 中以编程方式保存和检索下拉列表的选定值