简体   繁体   English

如何将特定值添加到 python 中的列表?

[英]How to add specific values to a list in python?

I want to do a simple plot between x and y where my x is x = np.arange(0,100) .我想在 x 和 y 之间做一个简单的 plot ,其中我的 x 是x = np.arange(0,100) My "y" values are mainly 0 but at some places it has a value of 0.5 and 1. Right now I have to write the whole values of y in a list as y = [0,0,0,0,0,0,0,0.5,0.5,0,0,......,1.....0] .我的“y”值主要是 0,但在某些地方它的值为 0.5 和 1。现在我必须将 y 的整个值写在列表中,如y = [0,0,0,0,0,0,0,0.5,0.5,0,0,......,1.....0] Is there a way where I can just write y = np.arange(0,100) to maintain the dimensionality of x and y and then input the specific values of y at the correct place?有没有一种方法可以只写y = np.arange(0,100)来保持 x 和 y 的维数,然后在正确的位置输入 y 的特定值? For example, at 8th position the y value is 0.5.例如,在第 8 个 position 处,y 值为 0.5。 So I want to write y = np.arange(0,100) and then input 0.5 at the 8th position of "y".所以我想写y = np.arange(0,100)然后在“y”的第8个position处输入0.5。 Otherwise I have to manually write the whole y list.否则我必须手动编写整个 y 列表。 Thank for the help in advance.提前感谢您的帮助。

You could just use你可以只使用
y = np.zeroes(100)
and then change the specific values just like you would do with lists by然后像您对列表所做的那样更改特定值
y[position where you would like to change] = 0.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM