简体   繁体   English

"为什么我的 for 循环只返回最后一个值?"

[英]Why is my for loop only returning last value?

for desc in database.desc:
    if sub in desc:
        station = desc.split(";")
        station.pop(0)
        station.pop(0)
        station.pop(1)
        if len(station) > 1:
            station.pop()
        print(station)
    else:
        ""

print(station)

The first print statement gives all the valeus I want.第一个打印语句给出了我想要的所有价值。 When I'm printing the statement oustide the for loop.当我在 for 循环之外打印语句时。 It only gives me the last value of the csv file.它只给了我 csv 文件的最后一个值。

因为在for循环的每次迭代中,您都将desc.split(";")的值分配给station所以它在每个时刻都保存当前值,当for循环结束时,它保存的值是最后一个,然后就是你打印它的时候。

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

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