简体   繁体   English

循环打印元组

[英]Printing tuples in a loop

When I print the range of my tuple it prints the first line and then goes through all 13 lines and keeps adding them to my tuple. 当我打印元组的范围时,它将打印第一行,然后遍历所有13行并将其添加到我的元组中。 This is what I want my program to do but I'm looking to just print the last tuple that has all the information in it not all of the ones which get added up... 这是我希望程序执行的操作,但我希望仅打印包含所有信息的最后一个元组,而不是所有累加的元组...

for i in range(1,13):
    tuple_list.append((line_list[i],year,header[i]))
    print(tuple_list)

For example if the tuples were: [1] [1,2] [1,2,3] [1,2,3,4] I only want to print the [1,2,3,4] tuple not the earlier ones. 例如,如果元组为: [1] [1,2] [1,2,3] [1,2,3,4]我只想打印[1,2,3,4]元组,而不是更早的那些。 Thanks 谢谢

For getting the last element, you need to specify -1 as index number. 为了获取最后一个元素,您需要指定-1作为索引号。

for i in range(1,13):
    tuple_list.append((line_list[i],year,header[i]))
print(tuple_list[-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM