添加到列表仅保存 python 中的最后一项 3

Appending to list saves only the last item in python 3

这是我为最新的计算物理期末考试(已经评分)编写的代码。在这段代码中,有一个名为 u_xt 的变量,它收集每个时间步 t 的分布 u(x)。在使用有限差分法计算新曲线的形状后,我使用附加函数(并且还尝试了创建数组并在每一步分配第 n 个元素的路径)并得到最终曲线(在 t = t_upper) 对于 n >= 1 的所有 u_xt[n] 值,u_xt[0] 是初始曲线。

我想不通为什么。

# animation adapted from http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

from numpy import linspace, zeros
import matplotlib.pyplot as plt
from matplotlib import animation

# setting up parameters
dt = 0.1
dx = 0.1
dtdx = dt / dx

# some tentative limits
x_upper = 10
x_lower = -10
t_upper = 10

# arrange variables
x = linspace(x_lower, x_upper, (x_upper-x_lower)/dx + 1 )
t = linspace(0, t_upper, t_upper/dt)

# initial condition
u = zeros(len(x))
u[135:140] = 1

# variable for full solution
u_xt = [] # <-- BELONGS TO THE DEVIL, SEE BELOW
u_xt.append(u)
u_old = u
u_new = zeros(len(u))

# value of u at next time step...
for n in range(1, int(t_upper/dt)):
    # we need i-th element for n+1
    for i in range(int((x_upper-x_lower)/dx)):
        # use old u to get new u at i-th grid
        u_new[i] = u_old[i] + 0.5*dtdx*(u_old[i+1] - u_old[i]) - 0.4*dt*u_old[i]

    # save new u 
    u_xt.append(u_new)
    # for the next step, newfound u is old u (so poetic)
    u_old = u_new
    # For some reason u_xt is saving only the last calculation (for t=t_upper)
    # It clearly is in the correct indentation!
    # Even u_xt[1] is the last curve. If I comment out the u_old = u_new line, then it saves only the first time-step calculation (as it should). But add that line and then u_xt becomes a dedicated servant of SATAN.

    # <RANT>
    # The solution (when looked at final snapshots with different t_upper values) is that of a diminishing translation to the left of the initial curve. But because u_xt is the variable of SATAN, it is not working. This refusal to carry out logic is at such magnitudes, it could replace the loss of free energy of our universe since the big friggin bang! Except, of course, u_xt is the variable of OATHBREAKER and will not do anything useful without eating your soul first.
    # </RANT>

# let's animate!

# set up figure, axis and plot element
fig = plt.figure()
ax = plt.axes(xlim=(-10,10), ylim=(0,1.1))
line, = ax.plot([], [], lw=2)

# initializing function
def init():
    line.set_data([], [])
    return line,

# animation function
def animate(k):
    line.set_data(x, u_xt[k])
    return line,

# call animator
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=int(t_upper/dt), interval=20, blit=True)

# save the animation (requires ffmpeg to be installed and callable from system path, can be commented out if not desired without ill effect)
anim.save('_q5.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.figure()
plt.plot(x, u_xt[-1], label="Final moment of the curve")
plt.show()

直接复制,否则就是多次引用:

u_xt.append(list(u_new))