python - 如何使用 'for' 循环在 Python 2.7 中迭代嵌套列表索引项?

标签 python list python-2.7 loops nested

我有一个这样的列表:

nList = [[0,0,0],[3,2,1]],\ [[]],\ [[100,1000,110]],\ [[0,0,0],[300,400,300],[300,400,720],[0,0,120],[100,0,1320],[30,500,1450]] 

并且需要以这样的方式存储输出,以便 '\' 之前的每个变量都像这样出现,例如,在第一个 '\' 之前:

0 metres, 0 metres, 0 seconds
3 metres, 2 metres, 1 seconds

到目前为止,我已经为每个 '\' 之前的各个索引提出了这个,例如对于 nList[4]:

outputList = []

for distance in nList[4]:
    outputLlist.append('{} metres, {} metres, {} seconds'.format(*distance))

但我如何创建一个 for 循环来遍历所有 nList 索引并以格式存储输出 'x' 米,'y' 米, 't' 秒 在空列表 outputList?

感谢您的帮助。

最佳答案

for i in range(1,len(nList)):
    for distance in nList[i]:
        outputLlist.append('{} metres, {} metres, {} seconds'.format(*distance))

我认为这会满足您的要求。

关于python - 如何使用 'for' 循环在 Python 2.7 中迭代嵌套列表索引项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29553629/

相关文章:

python - 将两个 LISTS 值的 SUM 添加到新 LIST

与常规字典相比,Python manager.dict() 非常慢

python - Windows 上的 Docker-Cloud CLI 错误

r - 从 R 中的列表中提取向量

python - django 应用程序的网络爬虫;我应该用 django 编写还是作为单独的脚本编写

python - 如何在使用 while 循环时将值 append 到字典中的列表?

python-2.7 - 计算一个单词在文本文件中出现的次数

python - 如何确保 __del__ 函数像通常(但不正确)预期的那样在 Python 类上调用?

c++ - 如何将 C++ 对象转换为 PyObject?

Python sort and sorted -- 列表的列表如何精确排序?