Python中的Append将额外的意外元素添加到计算值之间的列表中
我正在生成一些数字,每次生成一个数字时,我都想将其存储在列表中 守则:Python中的Append将额外的意外元素添加到计算值之间的列表中,python,list,loops,append,Python,List,Loops,Append,我正在生成一些数字,每次生成一个数字时,我都想将其存储在列表中 守则: 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.a
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之间
为什么?
H
和V
是同一个列表。为以下各项创建单独的列表:
H, V = [], []
行H=V=[]
只创建一个列表,然后将其分配给H
和V
:
>>> H = V = []
>>> H is V
True
>>> H.append(42)
>>> V
[42]
>>> H, V = [], []
>>> H is V
False
>>> H.append(42)
>>> V
[]
>>> a=b=[]
>>> a.append('hello b')
>>> a,b
(['hello b'], ['hello b'])
>>> a,b=[],[]
>>> a.append('sorry b')
>>> a,b
(['sorry b'], [])