如何将其写为列表理解?
for i in range(len(genes)):
if compareGenes(genes[i], target) > count:
best = genes[i]
count = compareGenes(genes[i], target)
答案 0 :(得分:5)
max
将是一个不错的方式。
count, best = max((compareGenes(k, target), k) for k in genes)
或者,使用key
参数max
:
best = max(genes, key=lambda k: compareGenes(k, target))