Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在这种情况下,避免混乱循环的最佳方法是什么?_Python_Oop - Fatal编程技术网

Python 在这种情况下,避免混乱循环的最佳方法是什么?

Python 在这种情况下,避免混乱循环的最佳方法是什么?,python,oop,Python,Oop,所以我想编辑一个由类组成的列表,这段代码模拟了两个动物互相吃掉。这个功能是关于一只动物(捕食者)吃掉另一只动物(猎物)。为了避免当我从列表中删除一只动物时循环变得“混乱”,我决定这样做: 最强的捕食者应该先吃,他应该吃最弱的猎物,检查他是否会尝试吃猎物的概率 def eat_predator(self): if len(self.specie_classes["Prey"]) > 0: self.specie_classes["Predator"].sort(

所以我想编辑一个由类组成的列表,这段代码模拟了两个动物互相吃掉。这个功能是关于一只动物(捕食者)吃掉另一只动物(猎物)。为了避免当我从列表中删除一只动物时循环变得“混乱”,我决定这样做: 最强的捕食者应该先吃,他应该吃最弱的猎物,检查他是否会尝试吃猎物的概率

def eat_predator(self):
    if len(self.specie_classes["Prey"]) > 0:
        self.specie_classes["Predator"].sort(
            key=lambda specie: specie.strength, reverse=True
        )
        for pred in self.specie_classes["Predator"]:
            prey_survivors= []
            food_apetite= pred.req['Apetite'] # This is how much food he wishes to eat
            current_food = 0
            self.specie_classes["Prey"].sort(
                key=lambda specie: specie.strength
            )
            for prey in self.specie_classes["Prey"]:
                if food_apetite >= current_food:
                    break

                if specie.determine_kill(prey):
                    current_food += prey.weight
                    pred.increase_eat_weight(prey.weight)
                    prey_survivors = [
                        surv_prey
                        for surv_prey in self.specie_classes["Prey"]
                        if surv_prey != prey
                    ]

            self.specie_classes["Prey"] = prey_survivors
这是我目前的解决方案,我的主要问题是每次我想更新包含猎物(动物)类的列表时,如何避免创建一个新的列表(而不破坏迭代)

这是我希望避免的:

d = list(range(10))
for n in d:
    print('Testing', n)
    if n % 2 == 0 or n % 3 == 0:
        d.remove(n)
print(d)

Testing 0
Testing 2
Testing 4
Testing 6
Testing 8
[1, 3, 5, 7, 9] 

一种方法是创建一个包含要删除的索引的列表/集合,然后通过排除这些索引重新创建
d

In [74]: d = list(range(10))                                                                                                                                                                                                                                                                                            

In [75]: deleteme = set()  # we'll track the indices to be deleted, here                                                                                                                                                                                                                                                                                             

In [76]: for i,n in enumerate(d): 
    ...:     if n%2==0 or n%3==0: 
    ...:         deleteme.add(i)  # delete this index
    ...:                                                                                                                                                                                                                                                                                                                

In [77]: d = [e for i,e in enumerate(d) if i not in deleteme]                                                                                                                                                                                                                                                           

In [78]: d                                                                                                                                                                                                                                                                                                              
Out[78]: [1, 5, 7]

我可以想出几个不同的选择

1) 在每只动物上都有一个名为“isAlive”的属性,并更新该属性,而不是删除该动物。通过这样做,列表的迭代将不会混乱


2) 创建列表的副本,然后仅从一个副本中删除动物。这样,原始的顺序和索引就得以保留。

这也是我在C#中处理这个问题的方式。