I asked a previous question here and got a great answer: How to assign items in nested lists a variable automatically in Python 2.7? to do the following when it came to putting up an output:
Previous Question:
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
Now, I need to format the distanceN variables so that trying to print distance4 for example would give the output:
>>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
Any help would be greatly appreciated. Thank you.
There is no need to convert nList
into anything; not into named variables, not into a dictionary. It works fine as a tuple (incidentally it is not a list - it is a tuple of lists). You could name it distances
instead.
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)
Output
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.