Reputation: 477
How do I write this as a list comprehension?
for i in range(len(genes)):
if compareGenes(genes[i], target) > count:
best = genes[i]
count = compareGenes(genes[i], target)
Upvotes: 2
Views: 125
Reputation: 179402
max
with a generator comprehension would be a nice way to go.
count, best = max((compareGenes(k, target), k) for k in genes)
Alternately, use a key
argument to max
:
best = max(genes, key=lambda k: compareGenes(k, target))
Upvotes: 5