s='abc'
ans,res=[],[]
for i in range(len(s)):
ans.append(i)
res.append(ans[:])
print(res,"
")
This appends ans
as the next item of list. If you see how ans
and res
change every iteration, you will understand how its is working.
Changes:
Iteration-0
ans- [0]
res- [[0]]
Iteration-1
ans- [0, 1]
res- [[0], [0, 1]]
Iteration-2
ans- [0, 1, 2]
res- [[0], [0, 1], [0, 1, 2]]
And for the other one:
ans,res=[],[]
for i in range(len(s)):
ans.append(i)
res.append(ans)
print("
",res)
This overwrites the complete list every iteration like:
Iteration-0
ans- [0]
res- [[0]]
Iteration-1
ans- [0, 1]
res- [[0, 1], [0, 1]]
Iteration-2
ans- [0, 1, 2]
res- [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…