gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:56:40 24 4
gpt4 key购买 nike

我有一个这样的列表:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com