【问题标题】:Combining selected items from different lists into a new one将不同列表中的选定项目组合成一个新项目
【发布时间】:2017-11-04 16:26:51
【问题描述】:

我有以下程序:

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

binary_list=[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1],
 [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1],
 [0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
 [1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0]]

def GeneratePopulation(random_pool, binary_list):
    individual = []
    population = []
    for gene in range(0, len(binary_list)):
        gene=binary_list[gene]
        for game in range (0, len(random_pool)):
            fitness=random_pool[game][2]
            counter=random_pool[game][3]
            individual=[gene,fitness,counter]
            population.append(individual)
    return(population)

输出:

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

我的目标是将 binary_list 中的元素基因(二进制向量)与 random_pool 列表中每个项目的最后两个元素结合起来,所以正确的结果是:

[[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [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], [3], [3]]]

我知道循环有问题,但我已经尝试了很多方法,但无法得到我想要的结果。谁能告诉我我做错了什么或想要修复它?

谢谢!

【问题讨论】:

    标签: python python-3.x list loops genetic-programming


    【解决方案1】:

    这是你的函数的版本,将给出预期的结果:

    def GeneratePopulation(random_pool, binary_list):
        individual = []
        population = []
        for ind in range(0,len(binary_list)):
           fitness = random_pool[ind][2]
           counter = random_pool[ind][3]
           gene = binary_list[ind]
           individual=[gene,fitness,counter]
           population.append(individual)
         return(population)
    

    在您的原始代码中,循环通过random_poolbinary_list 中的每个元素进行迭代——这不是必需的,并且会产生重复的结果。

    请注意,变量 fitnesscounterindividual 在这里是多余的 - 您可以在不创建它们的情况下获得相同的结果。您需要它们来做其他事情吗?

    【讨论】:

    • 这正是我想要的,太好了!关于变量,我正在命名它们,以便我知道在程序步骤中哪个是什么,但再想一想,我现在可能会删除它们,非常感谢!
    • 很高兴它起作用了 :) 是的,最好避免像这样的冗余变量,其中它们会造成在某处使用的错误印象。在这种情况下,您可以简单地将循环打包成一行population.append([binary_list[ind],random_pool[ind][2],random_pool[ind][3]])
    • @skrx 是的,我个人的选择是只保留使用的变量并在一行中注释什么是什么。但这是个人的,你说得有道理。
    【解决方案2】:

    一个更 Pythonic 的解决方案是 zip 列表,这样您就可以同时迭代 random_poolbinary_list 而不必使用索引。

    您也可以通过这种方式解压池:*_, fitness, counter = pool*_ 将第一个项目解包到变量_ 中,该变量通常用于不需要的值,最后两个被解包到变量fitnesscounter 中。

    def generate_population(random_pool, binary_list):
        population = []
        for pool, gene in zip(random_pool, binary_list):
            *_, fitness, counter = pool  # Unpack the pool. Ignore the first two items.
            population.append([gene, fitness, counter])
        return population
    

    使用list comprehension 或生成器表达式,您可以进一步简化函数。

    def generate_population(random_pool, binary_list):
        return [[gene, fitness, counter]
                for (*_, fitness, counter), gene in zip(random_pool, binary_list)]
    

    【讨论】:

    猜你喜欢
    • 2011-02-21
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多