根据变量索引顺序删除列表中的项目

时间:2017-11-23 17:39:03

标签: python python-3.x list random

def FitnessSelection(population, relative_fitness):
    selected_chromosomes = []
    selected1 = random.choices(population, weights = relative_fitness, k = 1)
    ind1 = selected1[0][1][0]
    del relative_fitness[ind1]
    del population[ind1] 
    selected2 = random.choices(population, weights = relative_fitness, k = 1)
    ind2 = selected2[0][1][0]
    del relative_fitness[ind2]
    del population[ind1] 
    selected = selected1, selected2
    selected_chromosomes = [i[0] for i in selected]
    return (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]]]

每个字符串的最后一个括号是索引计数器。我试图隔离计数器并将项目排除在同一位置,但是当我在之后检查人口时,他们并没有被排除在外。

有谁知道这种方法有什么问题?

P.S。 - 我很抱歉没有发布独立程序,我无法在这样的小样本中使用它。

谢谢!

0 个答案:

没有答案