Python | List,用于循环混乱

2024-04-16 14:41:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我不清楚为什么第二个输出没有给我任何结果,尽管同一行在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)

Tags: theforoutputelementprintslistrowsfinal
1条回答
网友
1楼 · 发布于 2024-04-16 14:41:00

检查这个python function return value。你知道吗

所以Python函数总是在结尾返回一些东西。在您的例子中,对于第二个输出,后面有一些行,如for循环等。但是对于第四个和第六个输出(假设您只取消注释其中一个),它碰巧是模块的最后一行代码,因此在退出程序执行期间,它返回从中得到的任何输出,然后在控制台上打印出来。你知道吗

相关问题 更多 >