Python |列表,For Loop混乱

时间:2017-10-14 16:51:54

标签: python python-3.x for-loop printing

我不清楚为什么第二个输出不会给我任何结果,尽管同一行给出了for循环后的结果。您可以逐个注释并检查输出。

提前致谢!

three_rows = ["Albuquerque,749", "Anaheim,371", "Anchorage,828"]
#print(three_rows)   #1st output
#three_rows[0]       #2nd output -- no response!

final_list = []
for row in three_rows:
    split_list = row.split(',')
    final_list.append(split_list)

#print(three_rows)   #3rd output -- prints the whole list(expected)
#three_rows[1]       #4th output -- prints the 2nd element(expected)
#print(final_list)   #5th output -- prints the whole list(expected)
#final_list[0]       #6th output -- prints the first element(expected)

1 个答案:

答案 0 :(得分:0)

选中python function return value

所以Python函数总是在最后返回一些东西。在你的情况下,对于第二个输出,有跟随它的行,如for循环等。但是对于第4和第6个输出(假设你只取消其中一个),它恰巧恰好是最后一个代码行模块,因此在退出程序执行期间,它返回来自该程序的任何输出,然后将其打印在控制台上。