简体   繁体   English

蟒蛇| 列表,For 循环混淆

[英]Python | List, For Loop confusion

I am not clear why second output doesnt give me any results although the same line gives me results after the for loop.我不清楚为什么第二个输出没有给我任何结果,尽管同一行在 for 循环后给了我结果。 You can comment out one by one and check the output.可以一一注释掉,查看输出。

Thanks in advance !提前致谢 !

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)

Check this python function return value .检查这个python 函数返回值

So Python function always returns something at the end.所以 Python 函数总是在最后返回一些东西。 In your case, for 2nd output, there are lines following it like the for loop etc. But for the 4th and 6th output(assuming you uncomment only one of them), it coincidentally happens to be the last code line of the module and hence during the exiting of the execution of your program, it returns whatever output comes from that and subsequently it gets printed on the console.在您的情况下,对于第二个输出,它后面有像for循环等的行。但是对于第 4 个和第 6 个输出(假设您只取消注释其中一个),它恰好是模块的最后一行代码,因此在退出程序执行期间,它会返回来自该程序的任何输出,然后将其打印在控制台上。

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

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