从命令行运行程序时,我得到奇怪的结果。我正在使用3.4.1(Anaconda发布)。
以下是代码段:
with
它以交互方式工作。但是,当我从中创建一个Python程序时(通过执行以下操作):
inFileList = [[1,2,3,4,5,6,7,8,9,10,11,12], 101,102,103,104,105,106,107,108,109,110,111,112]]
for eachRow in inFileList:
for eachItem in eachRow:
print(eachItem)
我在屏幕上得到一个完成正常的输出,但在周期的早期输出了数百个重复的“无意义”值。这是输出结束:
def main():
inFileList = [[1,2,3,4,5,6,7,8,9,10,11,12],[101,102,103,104,105,106,107,108,109,110,111,112]]
for eachRow in inFileList:
for eachItem in eachRow:
print(eachItem)
print (inFileList)
if __name__ == '__main__':
main()
现在我做了整理以提供更具代表性的输出,作为副作用我的错误消失了:
[hundreds of values that gradually change...] [...end of output is:]
367
367
367
367
367
1115
1115
1115
1115
1115
1115
1115
1115
1115
1115
1115
1115
1115
12
101
102
103
104
105
106
107
108
109
110
111
112
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112]]
我得到了预期的输出:
from __future__ import print_function
def main():
inFileList = [[1,2,3,4,5,6,7,8,9,10,11,12],[101,102,103,104,105,106,107,108,109,110,111,112]]
for eachRow in inFileList:
for eachItem in eachRow:
print(eachItem, end= ' ')
print (inFileList)
if __name__ == '__main__':
main()
所以我的问题已解决。但是我做错了什么,所以我可以避免将来犯同样的错误?