jay
jay

Reputation: 477

how to make this a list comprehension

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

Answers (1)

nneonneo
nneonneo

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

Related Questions