根据inside中的一个参数删除(或替换)多级列表中的项

2024-06-28 20:45:21 发布

您现在位置:Python中文网/ 问答频道 /正文

population=[[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [4], [0]],
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [1], [3]]]    

selected_chromosomes=[[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [5], [2]],
[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [3], [0]]]

child1=[0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]
child2=[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1]   



def PopulationAdjustment(population, selected_chromosomes):
    for game in range(0, len(selected_chromosomes)):
        if game in selected_chromosomes[game][2]==game in population[game][2]: 
            population.remove(game)
    return population  

因此,这里的目标是替换一个总体(列表)中的子对象的父对象,我的方法是删除父对象,然后根据相同的计数器追加子对象。列表的结构是[[chromosome],[fitness],[counter]],但是它们并不完全相同,因为我在选择过程中操纵了适应度以避免0概率。你知道吗

我正在尝试索引具有相同计数器的项并从列表中删除它们,然后下一步就是append子项。你知道吗

我一直在尝试一些不同的方法,但我不能让它正常工作,有没有想过如何解决这个问题?另外,如果有一种方法可以直接用子对象替换它们,而不必执行两个步骤(删除和附加),那也是非常受欢迎的。谢谢!!你知道吗


Tags: 对象方法ingame列表fordef计数器
1条回答
网友
1楼 · 发布于 2024-06-28 20:45:21

你说“我正在尝试索引的项目,有相同的计数器,并从列表中删除它们”。虽然这是可能的,但是建立一个包含你想要保留的染色体的新列表更容易(也更快),除非population是巨大的。你知道吗

我们首先扫描selected_chromosomes,将它们的计数器编号提取到一个集合中,以便快速查找它们。你知道吗

population=[
    [[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [4], [0]],
    [[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
    [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
    [[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [1], [3]],
]

selected_chromosomes=[
    [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [5], [2]],
    [[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [3], [0]],
]

def population_adjustment(population, selected_chromosomes):
    # Create a set of the counter numbers to remove
    drop = {u[-1][0] for u in selected_chromosomes}

    # Copy the wanted chromosomes to a new list
    return [u for u in population if u[-1][0] not in drop]

new_pop = population_adjustment(population, selected_chromosomes)
for row in new_pop:
    print(row)

输出

[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]]
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [1], [3]]

如果population列表很大,或者您有其他原因保留原始列表(例如,在不同的地方有多个引用),下面介绍如何删除不需要的列表。不过,我们必须小心。从正在迭代的列表中删除项是危险的,因为删除会干扰其余列表项的索引,如here所示。这有点像你坐在上面砍树枝。如果你插错了地方,坏事就会发生。;)最简单的方法是反向迭代列表。你知道吗

population=[
    [[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [4], [0]],
    [[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
    [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
    [[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [1], [3]],
]

selected_chromosomes=[
    [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [5], [2]],
    [[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [3], [0]],
]

def population_adjustment(population, selected_chromosomes):
    # Create a set of the counter numbers to remove
    drop = {u[-1][0] for u in selected_chromosomes}

    # Iterate backwards over population so we can safely delete sublists
    for i in range(len(population)-1, -1, -1):
        k = population[i][-1][0]
        if k in drop:
           del population[i] 

    # Since we mutate `population` we should return `None`, as is conventional in Python.
    # This return statement isn't necessary, since `None` is the default return value,
    # but it's nice to be explicit
    return None

population_adjustment(population, selected_chromosomes)
for row in population:
    print(row)

此代码生成的输出与上一版本相同,因此我将不再重复。你知道吗

相关问题 更多 >