What does return expression mean? python

I am working on a genetic algorithm, and I found code that works, and now I'm trying to figure it out, but I saw this return statement:

return sum(1 for expected, actual in zip(target, guess)
  if expected == actual)

What is he doing?

Here is the complete code:

main.py:

from population import *

while True:
    child = mutate(bestParent)
    childFitness = get_fitness(child)
    if bestFitness >= childFitness:
        continue
    print(child)
    if childFitness >= len(bestParent):
        break
    bestFitness = childFitness
    bestParent = child

population.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):
    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)

This is the complete code for a running genetic algorithm. I changed some parts to make them more readable for me.

The return statement is in the population.py file in the get_fitness function.

+4
source share
4 answers

Let me break this:

return sum(1 for expected, actual in zip(target, guess)
  if expected == actual)

can be written as:

total = 0
for i in range(len(target)):
    if target[i] == guess[i]:
        total = total + 1
return total

zip(a, b)creates a list of pairs of elements from aand b, for example:

zip([1, 2, 3], ['a', 'b', 'c'])

[(1, 'a'), (2, 'b'), (3, 'c')]. , zip(target, guess) target guess, target guess ..

for expected, actual in zip() zip(), ( target) expected, ( guess) actual.

1 ... if expected == actual " 1 zip(), expected actual.

sum() 1 for.

-! , . :

  • , . , Python, .
  • , Python , .., Python . Python: " , ", " 100 ".
+4

, , actual = expected. , , ,

+1

List Comprehension, zip().

, :

  • .
  • "" "" zip (target, guess). , 1 .
  • zip (target, guess).
  • 1.
  • .
+1

:

return sum(...)

, .

sum generator expression, .

1 for expected, actual in zip(target, guess) if expected == actual 1, , (expected == actual).

, : sum(1, 1, 1, 1, ...)

zip. zip ( !) ( !) . zip(['a', 'b', 'c'], [1, 2, 3]) , [('a', 1), ('b', 2), ('c', 3)].

, expected - [1, 2, 3], actual - [1, 1, 3], zip-, :

expected = [1, 2, 3]
actual = [1, 1, 3]
zip(expected, actual)   # [(1, 1), (2, 1), (3, 3)]

for, , - " ", target_list ().

, zip (1, 1), for expected, actual expected=1, actual=1.

, zip : a [0] b [0], a [1] b [1] .. for expected actual. for...if expected == actual , . , , , . 1. , 1. 1 0. 1 . 1, .

+1

Source: https://habr.com/ru/post/1684264/


All Articles