我的骨灰盒包含数字 1.3 和 0.9,我想每次模拟绘制 35 次替换。然后执行最终计算,将结果附加到列表中。总的来说,我想进行 10000 次模拟。
我的代码如下所示:
#Draw either 1.3 or 0.9
returns = [1.3,0.9]
#No. of simulations
simulations = 10000
#10000 for loops
for i in range(simulations):
lst = []
#each iteration should include 35 random draws with replacement
for i in range(35):
lst.append(random.choices(returns,1))
lst = np.array(lst)
#Do final calculation and append solution to list
ret = []
ret.append((prod(lst)^(1/35))-1)
我收到的错误是TypeError: 'int' object is not iterable
。我明白为什么它不起作用,因为我试图将整数转换为列表对象......但我只是不知道如何解决这个问题?
完整的堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-5d61655781f6> in <module>
9 #each iteration should include 35 random draws with replacement
10 for i in range(35):
---> 11 lst.append(random.choices(returns,1))
12
13 lst = np.array(lst)
~/opt/anaconda3/lib/python3.7/random.py in choices(self, population, weights, cum_weights, k)
355 total = len(population)
356 return [population[_int(random() * total)] for i in range(k)]
--> 357 cum_weights = list(_itertools.accumulate(weights))
358 elif weights is not None:
359 raise TypeError('Cannot specify both weights and cumulative weights')
TypeError: 'int' object is not iterable