how do i get this code to stop referring to its present value, so that the values already saved in the list are not changed to the present value?

``````Cm = 0x5A827999; Mm = 0x6ED9EBA1
Cr = 19; Mr = 17
Tm = [[0]*8]*24
Tr = [[0]*8]*24
for i in range(8):
for j in range(24):
Tm[j][i] = Cm
Cm = (Cm + Mm) % 2**32
Tr[j][i] = Cr
Cr = (Cr + Mr) % 32``````

if anyone cares, this is part of CAST-256

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by calccrypto

You lost me there! Can you explain yourself a little better?

the lists are supposed to have different values in each index. however, the computer keeps on referring to the last value of the "j" loop and replacing every Tr[x][0] value with Tr[23][0]'s value

say after the first loop, Tr is supposed to be

``````[
[1,0,0,0],
[5,0,0,0],
...
[11,0,0,0]
]``````

the code changes everything to

``````[
[11,0,0,0],
[11,0,0,0],
...
[11,0,0,0]
]``````

When you use Tm = [[0]*8]*24 you are creating 24 alias copies of an eight zero list. That means all 24 sublists have the same memory address.

To create the proper list of lists use this approach:

``````# create a 24 by 8 list of lists initialized with zero
zerolist24x8 = []
for x in range(24):
# create a fresh list of 8 zeros 24 times
zerolist8 = []
for y in range(8):
zerolist8.append(0)
zerolist24x8.append(zerolist8)``````

Now you can use module copy to make your copy of zerolist24x8, for instance:

``````import copy
Tm = copy.deepcopy(zerolist24x8)
Tr = copy.deepcopy(zerolist24x8)``````

thanks