2013-08-07 76 views
0

在Sage中工作基本上是python,我相信。我给了下面的代码。Python:一个变量被赋值并且没有改变另一个变化

def lfsr_1(regs,tabs): 
    I=regs 
    leng=len(I) 
    count=0 
    while True: 
     FB=0 
     print "Before" 
     print I 
     print regs 
     print temp 
     for i in range(0,leng): 
      FB=FB+tabs[i]*I[i] //Calculating the feedback value 
     for i in range(0,leng): 
      regs[leng-(i+1)]=regs[leng-(i+1)-1] //Shifting regs one bit to the right 
     I[0]=FB //Adding the feedback at the end 
     count=count+1 //Incrementing count value which will contain the periodicity 
     print "After" 
     print I 
     print regs 
     print temp 
     if (I==regs): //End when the initial state is repeated again. Now, count will contain the periodicity 
      break 

输入变量的初始化如下

tabs=[GF(2)(1),0,0,1,1,1] 
regs=[GF(2)(0),1,1,0,1,1] 
temp=regs 

然而,即时得到的输出为:

Before 
[0, 0, 1, 1, 0, 1] 
[0, 0, 1, 1, 0, 1] 
[0, 0, 1, 1, 0, 1] 
After 
[0, 0, 0, 1, 1, 0] 
[0, 0, 0, 1, 1, 0] 
[0, 0, 0, 1, 1, 0] 

不知道这是如何发生的“我”的变化连同“暂存器。 '我'在代码中永远不会改变。我的任务有什么问题吗?

附录:尝试实现线性反馈移位寄存器。代码是计算LFSR的周期性。 regs是初始状态,我用于检查regs何时再次返回到初始状态(计算周期性),temp只是一个测试变量,以查看是否另一个初始化变量也将被移位。

+0

如果你想提供一些关于你的最终目标是什么的信息,并且评论你的代码,它会使你更容易帮助你。 – pzkpfw

回答

2

当然与regs沿I的变化,因为你做这个任务:

I=regs 

现在Iregs引用相同的列表。这就是Python的工作原理。一个列表变量只是引用一个列表对象,它不包含整个列表对象。

下面是一个例子:

a = [1,2,3] 
b = a 
a[1] = 100 
print b 

也许你想使I复制的regs

尝试:

I = regs[:] 

当然,你可能会如果reg包含的对象有 “结构性共享”。

1

问题就出在线路

I=regs 

当你这样做,你不要复制的regs内容,你复制参考regs。从而使Iregs的数组相同。

相关问题