Question

Python 2.7.5

I can't find this on the other questions, so I'll ask...

The program is supposed to:

  1. Create a population of Organism()s.

  2. Select two random organisms from the population

  3. If those two organisms are both "black", delete them, and create a new "black" organism

What actually happens:

My program is giving a "list.remove(x): x not in list", when it very clearly IS in the list. The error occurs at only line 50(not 49): Python is saying that it can't remove it from the list, but it shouldn't be trying to remove it from the list in the first place(line 44).

I'm stumped at why it would do this, am I missing something obvious?

import random as r
import os
import sys
import time


class Organism(object):
    def __init__(self, color_code):
        self.color_code = color_code
        self.color = None
        if self.color_code == 0:
            self.color = 'black'
        if self.color_code == 1:
            self.color = 'white'




population_count = []
#Generates initial population
for organism in range(r.randint(2, 4)):
    org = Organism(0)
    population_count.append(org)

#Prints the color_traits of the different organisms

print "INITIAL"
for color in population_count:
    print color.color
print "INITIAL"




class PopulationActions(object):
    def __init__(self, pop):
        self.population_count = pop

    def Crossover(self, population):
        #Select 2 random parents from population
        parent1 = population[r.randint(0, (len(population) -1))]
        parent2 = population[r.randint(0, (len(population) -1))]
        #If both parents are 'black' add organism with black attribute and kill parents
        if parent1.color == "black" and parent2.color == "black":
            if parent1 in population and parent2 in population:
                org = Organism(0)
                population.append(org)
                print "__________ADDED ORGANISM_______________"
                population.remove(parent1)
                population.remove(parent2)
                print "__________KILLED PARENTS_______________"
        else:
            pass
#Main loop
pop = PopulationActions(population_count)
generation = 0
while generation <= 3:
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    pop.Crossover(population_count)
    #Print colors of current population
    for color in population_count:
        print color.color
    generation += 1
raw_input()
Was it helpful?

Solution

I suspect you're getting the error you describe when you randomly select the same Organism as both parents. You remove it from the list with your first call to list.remove, but the second one fails, as the Organism is already gone.

I'm not sure if you intend for it to be possible for the same organism to be picked twice. If so, you need to put a check on the second call to remove:

if parent2 is not parent1:
    population.remove(parent2)

If, on the other hand, you never want to pick the same Organism twice, you need to change how you're picking your parents. Here's a simple fix, though there are other ways to do it:

parent1, parent2 = random.sample(population, 2)

OTHER TIPS

What if parent1 == parent2? Then you remove both parent1 and parent2 in this line:

population.remove(parent1)

and parent2 really aren`t in list

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