我有一个字符串数字列表和一个for循环内的计数器。如果计数器在列表中,我会做一些事情,比如:
codes = ['123','1245','564','8920','57498']
f = open('path_to_file','r')
for lineno, line in enumerate(f, start=1):
if str(lineno) in codes:
print str(lineno) + ' is in the list'
问题是似乎if条件在某一点始终为真,因为一旦lineno = 123,它总是在if块内打印句子。
有人发现此代码有问题吗?谢谢
答案 0 :(得分:0)
我的代码如下:
In [8]: codes = ['1', '3', '4']
In [9]: f = open("test.py", "r")
In [10]: for lineno, line in enumerate(f, start=1):
....: if str(lineno) in codes:
....: print str(lineno) + ' is in the list'
....:
1 is in the list
3 is in the list
4 is in the list
看起来不错。