1

我正在生成一些数字,每次生成一个数字时,我都想将其存储在一个列表中。

编码:

for m in plaintexts:
    H = V = []

    for k in xrange(0, 256):
        di = m[i_temp1 : i_temp2]
        entry = int(sBox[int(di, 16) ^ k])
        print entry
        V.append(entry)
        print V
        H.append(bin(entry).count("1"))
    tempV.append(V)
    tempH.append(H)

不幸的是,我得到的完全不同:

89
[89]
250
[89, 4, 250]
240
[89, 4, 250, 6, 240]
71
[89, 4, 250, 6, 240, 4, 71]
130
[89, 4, 250, 6, 240, 4, 71, 4, 130]
202
[89, 4, 250, 6, 240, 4, 71, 4, 130, 2, 202]
125
[89, 4, 250, 6, 240, 4, 71, 4, 130, 2, 202, 4, 125]

我计算的值正在被添加,但在每个计算值之间总是添加一个随机数,这些随机值总是在 2-8 之间。

为什么?

4

2 回答 2

3

H并且V同一个列表。为每个创建单独的列表:

H, V = [], []

该行H = V = []仅创建一个列表,然后将其分配给两者HV

>>> H = V = []
>>> H is V
True
>>> H.append(42)
>>> V
[42]
>>> H, V = [], []
>>> H is V
False
>>> H.append(42)
>>> V
[]
于 2013-04-26T20:43:06.153 回答
0
>>> a=b=[]
>>> a.append('hello b')
>>> a,b
(['hello b'], ['hello b'])
>>> a,b=[],[]
>>> a.append('sorry b')
>>> a,b
(['sorry b'], [])
于 2013-04-26T20:48:12.890 回答