0

This is python 3.5 under Lubuntu. I'm creating a two-dimensional list of 0's (8 x 64) and then I assign a value to each position (dirty and fast way to store a cryptographic transformation, I'm just prototyping). When I try to retrieve the value stored in any position I get a different value than the one I stored (see doctest at the end of the code).

I'd like to know what's happening and why I'm not getting the results I'm expecting (if I can't find a solution I'm going to change it to a huge if-elif block).

_taulesS = 8 * [64 * [0]]

_taulesS[0][0] = 14
_taulesS[0][1] = 0
_taulesS[0][2] = 4
_taulesS[0][3] = 15
...
_taulesS[0][13] = 13
_taulesS[0][14] = 8
_taulesS[0][15] = 1
_taulesS[0][16] = 3
_taulesS[0][17] = 10
_taulesS[0][18] = 10
_taulesS[0][19] = 6
...
_taulesS[0][62] = 0
_taulesS[0][63] = 13
_taulesS[1][0] = 15
_taulesS[1][1] = 3
_taulesS[1][2] = 1
_taulesS[1][3] = 13
...

_taulesS[1][62] = 15
_taulesS[1][63] = 9
_taulesS[2][0] = 10
_taulesS[2][1] = 13
...
_taulesS[7][59] = 5
_taulesS[7][60] = 5
_taulesS[7][61] = 6
_taulesS[7][62] = 8
_taulesS[7][63] = 11


def taulesS(s, value):
    """
    >>> taulesS(0, 17)
    10

    Doctest fails, taulesS(0, 17) returns 12
    """
    return _taulesS[s][value]
vaultah
  • 44,105
  • 12
  • 114
  • 143
Jordi
  • 2,055
  • 1
  • 16
  • 34
  • You could print out the whole list of lists and realize the obvious... – Stefan Pochmann Oct 24 '17 at 20:06
  • The inner lists are ok, since they contain ints, which are immutable and therefore safe to duplicate, but you need a loop to make lists of lists `[64 * [0] for _ in range(8)]`. See the linked question for details. – PM 2Ring Oct 24 '17 at 20:08

0 Answers0