Question

so I need to make a program that generates generations of random equally fit alleles of a theoretical organism with a single gene.

I start with a list containing one mutated allele 2, and then I choose 3 alleles for the next generation, and append them to the list for the next generation

import random

p = [1,1,1,2]

from random import choice
n=len(p)-1

for i an range(n):
    p.append(choice(p))

problem is, this does not remove the allele that was not chosen. how can I adapt this program so that the non-chosen allele is removed from the list?

thanks

- edit

the out put of the program would look something like this

[1,1,1,2,1,2,1]

this would represent a population of 7 organisms with the 2 different alleles. This represents the 2nd generation as there are double alleles of the 3 chosen alleles. this simulated 2 offspring of the 3 chosen alleles. but the alleles that was not chosen (in this case 1) should not be present in this generation. so what I want to know is how to remove it from the list here

nb. sorry about being a bit verbose

Was it helpful?

Solution

import random 
n = 4 

A = [i for i in xrange(n)]
B = [1,1,1,2]
NextGeneration = [] 
for i in xrange(n-1):
    last = n - i - 1
    actualChoice =  random.randint(0,last)   
    NextGeneration.append(B[A[actualChoice]])  
    auxSwap = actualChoice 
    A[actualChoice] = A[last] 

OTHER TIPS

I'm not sure I understand your question fully, but here's an attempt at a solution:

first_gen = [1,1,1,2]
second_gen = first_gen + random.sample(first_gen, len(first_gen)-1)

Does that help at all?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top