Python数组/列表重置内部循环

时间:2017-11-15 16:41:46

标签: python arrays

from collections import deque
import itertools
lfsr = deque([])
taps = []
i=0
x=0
y=0
test=''

for i in itertools.product([0,1],repeat=15):
    lfsr = deque(i)
    #print(lfsr)
    while x < len(lfsr):
        while y < len(lfsr):
            taps = [x, y]
            #print (lfsr)
            y+=1
        x+=1

抱歉转发。

我的代码的简化版本有同样的问题。我试图将lfsr列表设置为等于二进制数,一旦设置了我想在嵌套循环中使用此值。 lfsr设置正确我相信当我取消注释掉第一个#print时,它会按照应有的方式打印,但是当我尝试在嵌套循环中打印它时,它的所有输出都是0。

是什么导致数组/列表设置为0并从最初设置时更改? 感谢

1 个答案:

答案 0 :(得分:1)

没有重置。问题是你的循环控制。由于您从未将xy重置为0,因此您输入while循环的唯一时间是所有0 s的出列时间。

from collections import deque
import itertools
lfsr = deque([])
taps = []
i=0 
test=''

for i in itertools.product([0,1],repeat=4):
    # reduced length to 4, to see the effects more easily

    lfsr = deque(i)
    print("TOP", lfsr)
    limit = len(lfsr)
    x=0
    while x < limit:
        y=0
        while y < limit:
            taps = [x, y]
            print ("MID", lfsr)
            y+=1
        x+=1

转换为for循环(这是正确的结构),以使这更容易。

for i in itertools.product([0,1],repeat=4):
    lfsr = deque(i)
    print("TOP", lfsr)
    limit = len(lfsr)

    for x in range(limit):
        for y in range(limit):
            taps = [x, y]
            print ("MID", lfsr)