简体   繁体   English

当我 append 我的空列表时,为什么我的列表长度为 1?

[英]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.

相关问题 从列表创建嵌套字典时,为什么字典的长度比每个列表的长度都短? - When I create a nested dictionary from a list, why is the length of my dictionary shorter than that of each list? 当我要求用户输入并将其附加到列表中时,为什么我的代码没有更改? - Why doesn't my code change when I ask for user input and append that to a list? 为什么添加新列表后数组中的所有列表都会更改? - Why do all the lists in my array change when I append a new list? 为什么我的列表打印不附加问题 - why my list print does not append problem 为什么我的列表在读取文件时返回空? - Why is my list returning empty when reading a file? 为什么我在自己的值列表中附加了值,但我的scoreList还是空的? 代码尝试最大化列表时的运行时 - Why is my scoreList empty even though I've appended values to it? Runtime when code tries to max list 为什么我的新列表在我应该得到答案时却输出了一个空列表 - Why is my new list outputting an empty one when I should be getting an answer 我的代码会附加一个列表,但不会打印出正确的长度 - My code will append a list but not print out the correct length 我的清单是空的,但我不知道为什么? - My list is empty but I can't figure out why? 如何附加到我的列表中? - How to append to my list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM