我想说
a[current] = value
Run Code Online (Sandbox Code Playgroud)
而不是说
a.append(value)
Run Code Online (Sandbox Code Playgroud)
因为我想表明当前值是值.前一个清单显示了这个更好.我来自C,所以我对python列表有点困惑.在CI预分配空间中,因此在分配值之前,[current]将存在并包含垃圾.我可以在Python中做类似的事情吗?
你可以做点什么
[0] * 10
Run Code Online (Sandbox Code Playgroud)
这将导致
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)
但你的方法可能不会非常"pythonic".如果切换到Python,你还应该考虑在python中思考.;-)