如何在 Python 2.7 中格式化元组列表?
How to format a tupled list in Python 2.7?
我在这里问了一个先前的问题并得到了很好的答案:How to assign items in nested lists a variable automatically in Python 2.7?在提出输出时执行以下操作:
上一个问题:
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]]
I need to assign automatic variables to the items before each '\'. for
example, distance1 = [[0,0,0],[3,2,1]], distance2=[[]], distance3=
[[100,1000,110]] etc. However, this needs to be automatic for each
distance'n' rather than me taking indexes from mList and assigning
them to variable distance'n
现在,我需要格式化 distanceN 变量,以便尝试打印 distance4 时会给出输出:
>>0 metres, 0 metres, 0 seconds
>>300 metres, 400 metres, 300 seconds
>>300 metres, 400 metres, 720 seconds
>>0 metres, 0 metres, 120 seconds
>>100 metres, 0 metres, 1320 seconds
>>30 metres, 500 metres, 1450 seconds
如有任何帮助,我们将不胜感激。谢谢。
不需要将nList
转换成任何东西;不进入命名变量,不进入字典。它作为一个元组工作得很好(顺便说一句,它是 而不是 一个列表——它是一个列表的元组)。您可以将其命名为 distances
。
distances = [[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]]
# "distance4" accessed by index 3 in tuple
for distance in distances[3]:
print '{} metres, {} metres, {} seconds'.format(*distance)
输出
0 metres, 0 metres, 0 seconds
300 metres, 400 metres, 300 seconds
300 metres, 400 metres, 720 seconds
0 metres, 0 metres, 120 seconds
100 metres, 0 metres, 1320 seconds
30 metres, 500 metres, 1450 seconds
我在这里问了一个先前的问题并得到了很好的答案:How to assign items in nested lists a variable automatically in Python 2.7?在提出输出时执行以下操作:
上一个问题:
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]]
I need to assign automatic variables to the items before each '\'. for example, distance1 = [[0,0,0],[3,2,1]], distance2=[[]], distance3= [[100,1000,110]] etc. However, this needs to be automatic for each distance'n' rather than me taking indexes from mList and assigning them to variable distance'n
现在,我需要格式化 distanceN 变量,以便尝试打印 distance4 时会给出输出:
>>0 metres, 0 metres, 0 seconds
>>300 metres, 400 metres, 300 seconds
>>300 metres, 400 metres, 720 seconds
>>0 metres, 0 metres, 120 seconds
>>100 metres, 0 metres, 1320 seconds
>>30 metres, 500 metres, 1450 seconds
如有任何帮助,我们将不胜感激。谢谢。
不需要将nList
转换成任何东西;不进入命名变量,不进入字典。它作为一个元组工作得很好(顺便说一句,它是 而不是 一个列表——它是一个列表的元组)。您可以将其命名为 distances
。
distances = [[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]]
# "distance4" accessed by index 3 in tuple
for distance in distances[3]:
print '{} metres, {} metres, {} seconds'.format(*distance)
输出
0 metres, 0 metres, 0 seconds 300 metres, 400 metres, 300 seconds 300 metres, 400 metres, 720 seconds 0 metres, 0 metres, 120 seconds 100 metres, 0 metres, 1320 seconds 30 metres, 500 metres, 1450 seconds