回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一百万圈要做。有没有办法更快地处理这个(以及一般的python循环)</p>
<pre><code>import numpy
#in a population of 1000 individuals of the same species, one individual has an advantageous mutation
#before, the average quantity of newborns per each 2 individuals per generation was 2 (no average change in pop)
popN, popM= 999, 1
period= 1000
reproductionDistN=[0.1, 0.2, 0.41, 0.19, 0.1] #expected value= 1.95
reproductionDistM=[0.1, 0.2, 0.39, 0.21, 0.1] #expected value= 2.05
def reproduce(pop, reproductionDist):
newborns=numpy.random.choice(numpy.arange(0,5), p=reproductionDist) #numpy.arange(0,5)=[0, 1, 2, 3, 4]
pop+=newborns
return pop
for t in range(period):
for n in range(popN): popN=reproduce(popN, reproductionDistN)
for m in range(popM): popM=reproduce(popM, reproductionDistM)
print("Population of N:",popN)
print("Population of M:",popM)
</code></pre>