这个返回语句是什么意思?python
我正在研究一个遗传算法,我发现了一个有效的代码,现在我正在努力理解,但我看到了这个返回语句:这个返回语句是什么意思?python,python,python-3.x,return,Python,Python 3.x,Return,我正在研究一个遗传算法,我发现了一个有效的代码,现在我正在努力理解,但我看到了这个返回语句: return sum(1 for expected, actual in zip(target, guess) if expected == actual) 它有什么作用 以下是完整的代码: main.py: from population import * while True: child = mutate(bestParent) childFitness = get_fit
return sum(1 for expected, actual in zip(target, guess)
if expected == actual)
它有什么作用
以下是完整的代码:
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
人口.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)
随机导入
geneSet=“abcdefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyz!”,1234567890-\=+!@$%^&*():“[]”
目标=输入()
def生成_父项(长度):
基因=[]
而len(基因)<长度:
样本大小=最小值(长度-len(基因),len(基因集))
基因.扩展(随机.样本(基因集,样本化))
parent=“”
对于基因中的i:
父项+=i
返回父级
def get_健身(猜测):
返回和(预期值为1,实际值为zip(目标值、猜测值)
如果预期==实际)
def突变(父代):
索引=随机。随机范围(0,len(父级))
childGenes=列表(父项)
newGene,alternate=随机样本(基因集,2)
childGenes[索引]=备用\
如果newGene==childGenes[索引]\
埃尔斯纽金
child=“”
对于childGenes中的i:
child+=i
返回儿童
def显示(猜测):
timeDiff=datetime.datetime.now()-startTime
适合度=获得适合度(猜测)
打印(str(猜测)+“\t”+str(适合)+“\t”+str(时间差))
random.seed()
最佳父对象=生成父对象(len(target))
最佳适应度=获得最佳适应度(最佳家长)
打印(最佳家长)
这是一个正在运行的遗传算法的完整代码。我修改了一些部分,使它更具可读性
return语句位于population.py文件中的get_fitness函数中。我认为它返回实际值=预期值的匹配总数。本质上,我认为它是检查算法模型能够正确预测的次数。它是一种使用函数的类型 基本上,守则是这样说的:
- 创建一个列表
- 从zip(target,guess)中检索变量“expected”和“actual”。如果它们相等,则在列表中添加1
- 对zip中的下一个值重复此操作(目标,猜测)
- 把所有的1加起来
- 返回此总和
return sum(1 for expected, actual in zip(target, guess)
if expected == actual)
可以写成:
total = 0
for i in range(len(target)):
if target[i] == guess[i]:
total = total + 1
return total
zip(a,b)
列出了a
和b
中的成对项目,如:
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
位表示“如果expected
中的值等于actual
中的值,则为zip()中的每个项目发出1的值。”
sum()
将for循环中的1
值相加
塔达!现在您有了预期值和实际值相同的项目计数。这样写有几个原因:
它非常简洁,但富有表现力。编写大量Python的人可以浏览一下并理解它
它可能非常快,因为Python解释器正在处理循环、条件等,对Python解释器的改进可以使代码更快,而不必理解整个程序。基本上,您告诉Python的是“我希望完成这件事”,而不是“这里有100个小步骤来完成这件事”
发生了几件事:
return sum(...)
这意味着您正在返回一个数字
sum
的内部是一个函数,它创建并运行一个隐式循环
在这种情况下,1 for expected,actual In zip(target,guess),if expected==actual
创建一系列1
值,每次保护条件为true时创建一个条目(expected==actual
)
因此,这一行实际上创建了如下代码:sum(1,1,1,1,…)
在生成器表达式中,有一个调用。zip表达式将获取两个(或更多!)序列,并将它们转换为具有两个(或更多!)值的元组的单个序列。也就是说,zip(['a','b','c'],[1,2,3])
将生成一个序列作为其输出,如[('a',1),('b',2),('c',3)]
因此,如果您的预期的是[1,2,3]
,而您的实际的是[1,1,3]
,您将得到如下结果:
expected = [1, 2, 3]
actual = [1, 1, 3]
zip(expected, actual) # [(1, 1), (2, 1), (3, 3)]
生成器表达式包含一个用于
的,该表达式使用曾经被称为“tuple unpacking”的方法,从单个聚合(tuple)值中为其分配多个目标
因此,当zip表达式生成(1,1)
时,expected,actual
的将其解压为expected=1,actual=1
因此,zip
采用两个等长序列,并将它们对应的元素配对:a[0]与b[0],a[1]与b[1],等等。for
生成器表达式将这些元素分配到称为预期的和实际的变量中。for…if
生成器条件部分比较预期==实际值,并生成值或不生成值。因此,结果序列的长度保证小于或等于输入序列的长度,但您不知道它的长度。生成器的表达式部分只是1
。所以你有一个1的可变长度序列。它不是1或0。要么是1,要么就是没有入口。把所有的1加起来,就是结果。它被称为