2013-08-07 77 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的數組相同。

相關問題