简体   繁体   English

enumerate()的奇怪行为

[英]Strange behavior from enumerate()

So I'm reading in this PPM file, reading it line-by-line, manipulating it here and there, and writing it out new lines to another file. 因此,我正在读取此PPM文件,逐行读取,在此处和此处进行操作,并将新行写到另一个文件中。 I tried producing a minimal working example to reproduce the behavior, but when I write a minimal example the strange behavior goes away--but I can't figure out what part of this is causing the strange behavior. 我尝试制作一个最小的工作示例来重现该行为,但是当我编写一个最小的示例时,奇怪的行为就消失了,但是我无法弄清楚这是什么部分导致了奇怪的行为。 So the full code is below. 因此,完整的代码如下。

def flip_horizontal(infile, outfile):
with open(os.getcwd() + '\\' + infile, 'r') as f:
    outfile = open(os.getcwd() + '\\' + outfile, 'w')
    rgbCounter = 0
    for i, line in enumerate(f):
        if i < 3:
            outfile.write(line)
        if i == 1:
            width = int(line.split()[1])
            lineList = [None for i in range(width*3)]
            if width > 1024:
                print "Image size too large:  Buffer can only store 1024 \
                    pixels at a time.  Aborting negate_red."
                break
        if i > 2:
            print line
            for integer in line.split():
                if rgbCounter%3 == 0:
                    lineList[width*3-rgbCounter-3] = integer
                elif rgbCounter%3 == 1:
                    lineList[width*3-rgbCounter-1] = integer
                else:
                    lineList[width*3-rgbCounter+1] = integer
                rgbCounter += 1
                if rgbCounter == width*3:
                    outfile.write(' '.join(lineList))
                    outfile.write('\n')
                    rgbCounter = 0
    outfile.close()

And here's the problem: When I run this code, the "print" line after if i > 2: also prints line 2 (or rather, line 3, the line indexed at 2)! 这就是问题所在:当我运行此代码时, if i > 2:之后的“打印”行也会打印第2行(或者更确切地说,是第3行,索引为2的行)! In a more minimal example, if I just say 在一个最小的例子中,如果我只是说

...
if i > 2:
    print line
...

It will only print lines 4 and after, but something about the rest of the code that I'm actually running makes it print line 3. Any ideas why and how to fix this? 它只会打印第4行及其后的内容,但是我实际运行的其余代码会使它打印第3行的内容。您知道为什么以及如何解决此问题吗?

Here's a sample input: 这是一个示例输入:

P3
4 4
255
49   49   49    100   100   100     0   200   0     0   0   0 
100  100  100   100   0     0       200 200   200   255 255 255
200  100  0     0     100   200     0   0     0     50  50  50
0    0    0     0     0     0       0   0     0     0   0   0

and when I run the code I get the printed lines 当我运行代码时,我得到了打印的行

4 4

49   49   49    100   100   100     0   200   0     0   0   0 

100  100  100   100   0     0       200 200   200   255 255 255

...

The first line, containing "4 4" shouldn't be there. 第一行不应包含“ 4 4”。

The problem is that you are changing the value of i in the statement 问题是您正在更改语句中i的值
[None for i in range(width*3)] . [None for i in range(width*3)] i will be width*3 - 1 when it encounters if i > 2 , which then evaluates to True . iwidth*3 - 1 ,当它遇到if i > 2 ,然后评估为True
Simply changing the name of the variable ( [None for j in range(width*3)] ) will do the trick. 只需更改变量名( [None for j in range(width*3)] )即可解决问题。 Note that i > 2 means the first line to be printed will be the fourth one (index 3). 请注意, i > 2表示要打印的第一行将是第四行(索引3)。 If you want the third one to print, you should use if i > 1 . 如果要打印第三个,则应使用if i > 1
minimal example showing the behaviour: 显示行为的最小示例:

def test():
    a = ['a', 'b', 'c', 'd']
    for i, char in enumerate(a):
        if i==1:
            var = [None for i in range(4)]
        if i>2:
            print(i, char)
             test()

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

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