【发布时间】:2021-11-10 20:53:19
【问题描述】:
我有一个列表列表:
0 [[[20.5973832, 52.8685205], [20.5974081, 52.86...
1 [[[21.0072715, 52.2413049], [21.0072603, 52.24...
2 [[[18.8446673, 50.4508418], [18.8447041, 50.45...
3 [[[18.8649393, 50.4483321], [18.8649802, 50.44...
4 [[[16.7529018, 53.1424612], [16.7528965, 53..
我需要遍历列表的每个元素(每个坐标号),并确保它在句点后有 7 位数字。如果没有,我需要在末尾用 0 填充,使其在句点后有 7 位数字。
每个数字都是一个浮点数,但我可以将其转换为字符串以使用 len() 函数。
我的代码是:
for a in list_of_lists:
for b in a:
for c in b:
for d in c:
if(len(str(d))<10):
d = str(d).ljust(10-len(str(d)), '0')
我得到的错误是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-46-70d9d844f41b> in <module>
3 for a in list_of_lists:
4 for b in a:
----> 5 for c in b:
6 for d in c:
7 if(len(str(d))<10):
TypeError: 'float' object is not iterable
实现这一目标的更好方法是什么?
【问题讨论】:
-
这真的是两个问题。 (1) 如何格式化具有特定位数的数字; (2) 如何正确地迭代这个特定的列表结构
-
对于(1),见stackoverflow.com/questions/8885663/…;对于(2),您需要显示minimal reproducible example。
-
@Feyzi-Bagirov,答案对你没有帮助?
标签: python python-3.x iteration nested-lists