计算每个更新值及其对应的随机数,然后绘制它们
Calculate each updated value and its corresponding random and then graph them
import random
#import matplotlib.pyplot as plt
a1 = ['','','?','']
a2 = [10,25,43.34,90]
i = 0
i_array = []
while i < 10:
i_array.append(i)
i = i + 1
r = random.random()
for i, j in enumerate(a1):
if j == '?':
print(a2[i]*r)
a3 = a2[i]*r
plt.line(r,a3)
我在 a1 中的问号可以在这四个地方中的任何一个地方。所以,需要改变a2中对应的值。
答案:
随机导入
#import matplotlib.pyplot 作为 plt
a1 = ['','','?','']
a2 = [10,25,43.34,90]
xarray=[]
yarray=[]
i = 0
i_array = []#probably can delete this, I don't see any reason for it
for i in range(0,10):#use a for loop instead
i_array.append(i)
r = random.random()
a3 = a2[a1.index('?')]*r#index here instead of the for loop
print(a3)#since your assigning a3 anyway, might as well print that
xarray.append(r)#plot needs arrays
yarray.append(a3)
plt.plot(xarray,yarray)#plot your arrays
你能详细说明你想在这里做什么吗?看起来您正在尝试根据“?”的位置在 a2 中选择一个值。包含在 a1 中,然后乘以 a2[ 的索引?在 a1] 中用一个随机数绘制它,并用 y 轴上的乘积和 x 轴上的随机数绘制它。基于这个假设,有几种选择。最明显的是使用 index() 方法,请参阅以下问题:Python: finding an element in an array. Alternatively, if the '?' is meant to also be randomly placed in a1 then it is simpler to find the index of a2 randomly rather than using the two lists. Do this with the following a2[random.ranint(0, len(a2)-1)]
. (documentation here: https://docs.python.org/2/library/random.html) 此外,我不是 pyplot 专家,但看起来您对 plt.line(r,a3)
的调用可能无法正常工作你想要它。根据我认为您想做的事情,您可能想在循环的每次迭代中将 r 和 a3 附加到两个单独的列表(例如 rlist、a3list),然后调用 plt.plot(rlist, a3list)
。最后,您的 while 循环没有错,但您似乎将其用作 for 循环,因此您最好还是这样做(for i in range(0,10):
)
import random
#import matplotlib.pyplot as plt
a1 = ['','','?','']
a2 = [10,25,43.34,90]
i = 0
i_array = []
while i < 10:
i_array.append(i)
i = i + 1
r = random.random()
for i, j in enumerate(a1):
if j == '?':
print(a2[i]*r)
a3 = a2[i]*r
plt.line(r,a3)
我在 a1 中的问号可以在这四个地方中的任何一个地方。所以,需要改变a2中对应的值。 答案: 随机导入 #import matplotlib.pyplot 作为 plt
a1 = ['','','?','']
a2 = [10,25,43.34,90]
xarray=[]
yarray=[]
i = 0
i_array = []#probably can delete this, I don't see any reason for it
for i in range(0,10):#use a for loop instead
i_array.append(i)
r = random.random()
a3 = a2[a1.index('?')]*r#index here instead of the for loop
print(a3)#since your assigning a3 anyway, might as well print that
xarray.append(r)#plot needs arrays
yarray.append(a3)
plt.plot(xarray,yarray)#plot your arrays
你能详细说明你想在这里做什么吗?看起来您正在尝试根据“?”的位置在 a2 中选择一个值。包含在 a1 中,然后乘以 a2[ 的索引?在 a1] 中用一个随机数绘制它,并用 y 轴上的乘积和 x 轴上的随机数绘制它。基于这个假设,有几种选择。最明显的是使用 index() 方法,请参阅以下问题:Python: finding an element in an array. Alternatively, if the '?' is meant to also be randomly placed in a1 then it is simpler to find the index of a2 randomly rather than using the two lists. Do this with the following a2[random.ranint(0, len(a2)-1)]
. (documentation here: https://docs.python.org/2/library/random.html) 此外,我不是 pyplot 专家,但看起来您对 plt.line(r,a3)
的调用可能无法正常工作你想要它。根据我认为您想做的事情,您可能想在循环的每次迭代中将 r 和 a3 附加到两个单独的列表(例如 rlist、a3list),然后调用 plt.plot(rlist, a3list)
。最后,您的 while 循环没有错,但您似乎将其用作 for 循环,因此您最好还是这样做(for i in range(0,10):
)