如何用循环填充数组?

2024-09-25 16:32:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是:

我们研究函数 f(x)=ln(x)。 我们想用x和f(x)的值分别填充两个数组x和y。 在间隔[1,10]中使用101个均匀间隔的x值。首先创建数组x 长度正确的y,包含所有零。然后计算并填写 x和y中的每个元素都有一个for循环

我试图用这种方式解决这个问题,但考虑到我的印刷品,它似乎不正确:

    "Problem 6.1 - Import arrays; loop version"
import numpy as np
from numpy import log

x=np.linspace(1,10,101)

x_array=["0"]*101
y_array=["0"]*101

for x in range(len(x)):
    f=log(x)
    x_array.append(x)
    y_array.append(f)

有人知道如何解决这个问题吗


Tags: 函数importnumpylog元素for间隔np
2条回答

您可以使用range()生成x_array,然后使用math.log()相应地填充y_array

from math import log

x_array = list(range(1, 11))
y_array = []
for val in x_array:
    y_array.append(log(val))

print(x_array)
print(y_array)

或者,简而言之,使用列表理解:

from math import log

x_array = list(range(1, 11))
y_array = [log(val) for val in x_array]

print(x_array)
print(y_array)

您实际上不需要填充数组^{}为您生成一个均匀间隔的numpy数组。但是,根据您的要求,您可以这样做:

解决方案1:循环方法

import numpy as np
from numpy import log

#filling zeros
x_array = np.zeros(101)
y_array = np.zeros(101)

#generating uniformly spaced numbers
x=np.linspace(1,10,101)

#filling the array with loop
for i in range(0,len(x)):
    x_array[i] = x[i] #filling  x_array
    y_array[i] = log(x[i]) #filling y_array

print(x_array,y_array)

解决方案2:列表理解方法

import numpy as np
from numpy import log

#filling zeros
x_array = np.zeros(101)
y_array = np.zeros(101)

#generating uniformly spaced numbers
x=np.linspace(1,10,101)

#filling the array with list comprehension
x_array = [ x[i] for i in range(0,len(x))]
y_array = [ log(x[i]) for i in range(0,len(x))]

print(len(x_array),len(y_array))
print(x_array,y_array)

输出

#length
101 101

#x_array
[1.0, 1.09, 1.18, 1.27, 1.3599999999999999, 1.45, 1.54, 1.63, 1.72, 1.81, 1.9, 1.99, 2.08, 2.17, 2.26, 2.3499999999999996, 2.44, 2.5300000000000002, 2.62, 2.71, 2.8, 2.8899999999999997, 2.98, 3.07, 3.16, 3.25, 3.34, 3.4299999999999997, 3.52, 3.61, 3.6999999999999997, 3.79, 3.88, 3.9699999999999998, 4.0600000000000005, 4.15, 4.24, 4.33, 4.42, 4.51, 4.6, 4.6899999999999995, 4.779999999999999, 4.869999999999999, 4.96, 5.05, 5.14, 5.2299999999999995, 5.32, 5.41, 5.5, 5.59, 5.68, 5.77, 5.859999999999999, 5.95, 6.04, 6.13, 6.22, 6.31, 6.3999999999999995, 6.49, 6.58, 6.67, 6.76, 6.85, 6.9399999999999995, 7.029999999999999, 7.12, 7.21, 7.3, 7.39, 7.4799999999999995, 7.569999999999999, 7.66, 7.75, 7.84, 7.93, 8.02, 8.11, 8.2, 8.29, 8.379999999999999, 8.469999999999999, 8.559999999999999, 8.649999999999999, 8.739999999999998, 8.83, 8.92, 9.01, 9.1, 9.19, 9.28, 9.37, 9.459999999999999, 9.549999999999999, 9.64, 9.73, 9.82, 9.91, 10.0] 

#y_erray
[0.0, 0.08617769624105241, 0.16551443847757333, 0.23901690047049992, 0.30748469974796055, 0.371563556432483, 0.43178241642553783, 0.4885800148186709, 0.5423242908253617, 0.5933268452777344, 0.6418538861723947, 0.688134638736401, 0.7323678937132266, 0.7747271675523681, 0.8153648132841944, 0.8544153281560675, 0.8919980393051105, 0.928219302739429, 0.9631743177730056, 0.9969486348916096, 1.0296194171811581, 1.0612565021243408, 1.091923300517313, 1.1216775615991057, 1.1505720275988207, 1.1786549963416462, 1.205970806988609, 1.2325602611778486, 1.2584609896100056, 1.2837077723447896, 1.3083328196501787, 1.332366019094335, 1.355835153635182, 1.378766094699099, 1.4011829736136414, 1.423108334242607, 1.4445632692438664, 1.4655675420143985, 1.4861396960896067, 1.506297153514587, 1.5260563034950492, 1.5454325824581878, 1.5644405465033644, 1.5830939370944983, 1.601405740736836, 1.6193882432872684, 1.6370530794670737, 1.6544112780768314, 1.6714733033535532, 1.6882490928583902, 1.7047480922384253, 1.7209792871670078, 1.7369512327330598, 1.7526720805200082, 1.7681496035889213, 1.7833912195575383, 1.7984040119467235, 1.81319474994812, 1.827769906751088, 1.8421356765531218, 1.856297990365626, 1.8702625307159986, 1.884034745337226, 1.8976198599275322, 1.9110228900548727, 1.9242486522741338, 1.937301774518713, 1.9501867058225735, 1.9629077254238845, 1.9754689512968577, 1.9878743481543455, 2.0001277349601105, 2.012232791986386, 2.024193067449357, 2.0360119837525, 2.0476928433652555, 2.0592388343623163, 2.0706530356467567, 2.081938421878423, 2.0930978681273213, 2.1041341542702074, 2.1150499691472033, 2.1258479144939915, 2.136530508663963, 2.1471001901536506, 2.157559320943788, 2.167910189667444, 2.178155014615869, 2.188295946591918, 2.1983350716202463, 2.2082744135228043, 2.2181159363675955, 2.2278615467981093, 2.2375130962503307, 2.2470723830637866, 2.2565411544926386, 2.265921108622454, 2.2752138961979136, 2.2844211223663744, 2.2935443483418965, 2.302585092994046]

编辑

添加打印功能:

#add this code below the above code if you want a plot
import matplotlib.pyplot as plt

plt.plot(x_array,y_array)
plt.title(r'$f(x)=log(x)$')
plt.show()

输出

enter image description here

相关问题 更多 >