I have a list of randomly generated 0's and 1's. I need to pick a single bit and flip it to its opposite: either a zero to a one or a one to a zero. This is part of a genetic algorithm problem in Python.
What I have tried:
import random as randint pop_size = int(input('Enter a population size:')) length = int(input('Enter an organism length:')) orgs =  for i in range(pop_size): org =  for a in range(length): org.append(randint.randint(0,1)) orgs.append(org) orgs.sort(reverse=True) print orgs new_orgs =  new_org = orgs[0:(pop_size/2):1] #look at user input for 50 or 25, etc new_orgs.append(new_org) print new_orgs import random as randint x = randint.choice(new_orgs) j = randint.randrange(len(x)) x[j] = int(not x[j]) print x
The code above returns this:
Enter a population size: 4 Enter an organism length: 3 [[1, 1, 1], [1, 0, 1], [1, 0, 0], [0, 1, 1]] #sorted list by sum [[[1, 1, 1], [1, 0, 1]]] # half of list which has 'higher' fitness [[1, 1, 1], 0] #output
I need to get something like this:
[[1, 0, 1], [1,1,1,]] #in regards to output