【问题标题】:What is this assignment? Python这个任务是什么? Python
【发布时间】:2023-03-24 20:18:01
【问题描述】:

我从一本书中复制了一个遗传密码,我发现了这个作业:

childGenes[index] = alternate \
    if newGene == childGenes[index] \
    else newGene

完整的代码是这样的: main.py:

from population import *

while True:
    child = mutate(bestParent)
    childFitness = get_fitness(child)
    if bestFitness >= childFitness:
        continue
    print(str(child) + "\t" + str(get_fitness(child)))
    if childFitness >= len(bestParent):
        break
    bestFitness = childFitness
    bestParent = child

人口.py:

import random

geneSet = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!.,1234567890-_=+!@#$%^&*():'[]\""
target = input()

def generate_parent(length):
    genes = []
    while len(genes) < length:
        sampleSize = min(length - len(genes), len(geneSet))
        genes.extend(random.sample(geneSet, sampleSize))
    parent = ""
    for i in genes:
        parent += i
    return parent

def get_fitness(guess):
    total = 0
    for i in range(len(target)):
        if target[i] == guess[i]:
            total = total + 1
    return total
    """
    return sum(1 for expected, actual in zip(target, guess)
        if expected == actual)
    """

def mutate(parent):
    index = random.randrange(0, len(parent))
    childGenes = list(parent)
    newGene, alternate = random.sample(geneSet, 2)
    childGenes[index] = alternate \
        if newGene == childGenes[index] \
        else newGene

    child = ""
    for i in childGenes:
        child += i

    return child

def display(guess):
    timeDiff = datetime.datetime.now() - startTime
    fitness = get_fitness(guess)
    print(str(guess) + "\t" + str(fitness) + "\t" + str(timeDiff))

random.seed()
bestParent = generate_parent(len(target))
bestFitness = get_fitness(bestParent)
print(bestParent)

分配在population.py 中,在mutate 函数中。我从未见过这种变量赋值。这是什么? “\”符号是什么意思?

【问题讨论】:

    标签: python python-3.x variable-assignment


    【解决方案1】:

    作业已拆分为多行。反斜杠joins the lines。赋值可以改写为:

    childGenes[index] = alternate if newGene == childGenes[index] else newGene
    

    这是一个conditional expression(有时称为“三元运算符”),相当于:

    if newGene == childGenes[index]:
        childGenes[index] = alternate
    else:
        childGenes[index] = newGene
    

    【讨论】:

      【解决方案2】:

      翻译成:

      if newGene == childGenes[index]:
         childGenes[index] = alternate
      else:
         childGenes[index] = newGene
      

      【讨论】:

        【解决方案3】:

        它是:

        if newGene == childGenes[index]:
            childGenes[index] = alternate
        else:
            childGenes[index] = newGene
        

        【讨论】:

          【解决方案4】:

          这里的反斜杠是一个简单的换行符。因此,python 解释器将这些行读为一个:

          childGenes[index] = alternate if newGene == childGenes[index] else newGene

          这里讨论了单行条件分配:

          https://stackoverflow.com/questions/7872838/one-line-if-condition-assignment

          【讨论】:

            猜你喜欢
            • 2021-12-29
            • 1970-01-01
            • 1970-01-01
            • 2013-07-28
            • 1970-01-01
            • 2012-03-04
            • 2016-08-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多