[英]Why is my list of length 1 when i append my empty list?
I am trying to append an empty list with outputs of a function with a range of input values.我正在尝试 append 一个空列表,其中 function 的输出具有一系列输入值。 I then want to plot the inputs against the outputs.
然后我想 plot 输入与输出。 However, the dimension of the resulting list is said to be 1, but when I print the list itself, it seems to show many entries.
但是,结果列表的维度据说是 1,但是当我打印列表本身时,它似乎显示了很多条目。 I therefore can not plot the input vs output due to differences in dimension.
因此,由于尺寸差异,我不能 plot 输入 vs output。 Any help would be greatly appreciated.
任何帮助将不胜感激。
PS - I'm very much in the early stages of learning Python again so I'm sure this is a rudimentary problem. PS - 我非常处于学习 Python 的早期阶段,所以我确信这是一个基本问题。
import math
import numpy as np
import pylab
from sympy import *
Um,x,x0,alpha=symbols('Um x x0 alpha', real=True)
U = Um*((1-exp(-alpha*(x-x0)))**2-1)
F=-U.diff(x)
x = np.arange(0,30,0.1)
y=[]
y.append(-2*Um*alpha*(1 - exp(-alpha*(x - x0)))*exp(-alpha*(x - x0)))
alpha = 2.287
x0=1.2
Um=7.37
exp=math.exp
print(len(y))
print(y)
pylab.plot(x,y)
pylab.show()
Since the element that you want to append to y
is a list, it is appended as single element, which contains several items.由于您想要 append 到
y
的元素是一个列表,因此它作为单个元素附加,其中包含多个项目。 That's why the length of y
is 1.这就是
y
的长度为 1 的原因。
In order to add the elements of list into another list, you can use extend
, which is basically a list concatenation.为了将列表的元素添加到另一个列表中,您可以使用
extend
,这基本上是一个列表连接。
Change append
to extend
:将
append
更改为extend
:
y.extend(-2*Um*alpha*(1 - exp(-alpha*(x - x0)))*exp(-alpha*(x - x0)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.