[英]list.remove(x): x not in list
Python 2.7.5 Python 2.7.5
I can't find this on the other questions, so I'll ask... 我在其他问题上找不到这个,所以我会问...
The program is supposed to: 该程序应该:
Create a population of Organism()s. 创建大量的有机体。
Select two random organisms from the population 从种群中选择两种随机生物
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. 当程序很明显在列表中时,我的程序给出的是“ list.remove(x):x不在列表中”。 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).
该错误仅发生在第50行(不是49行):Python表示无法将其从列表中删除,但它不应该首先尝试从列表中将其删除(第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()
I suspect you're getting the error you describe when you randomly select the same Organism
as both parents. 我怀疑当您随机选择与父母双方相同的
Organism
时,您会遇到错误。 You remove it from the list with your first call to list.remove
, but the second one fails, as the Organism
is already gone. 您第一次调用
list.remove
将其从列表中删除,但是第二次失败了,因为Organism
已经消失了。
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
: 如果是这样,您需要检查第二个电话以
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 parent
s. 另一方面,如果您不想两次采摘相同的
Organism
,则需要更改采摘parent
的方式。 Here's a simple fix, though there are other ways to do it: 这是一个简单的修复程序,尽管还有其他方法可以做到:
parent1, parent2 = random.sample(population, 2)
What if parent1 == parent2? 如果parent1 == parent2怎么办? Then you remove both parent1 and parent2 in this line:
然后在此行中删除parent1和parent2:
population.remove(parent1)
人口。去除(父母1)
and parent2 really aren`t in list 和parent2确实不在列表中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.