简体   繁体   English

如何使它成为列表理解

[英]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) 

max with a generator comprehension would be a nice way to go. 用生成器理解max可以是一个不错的方法。

count, best = max((compareGenes(k, target), k) for k in genes)

Alternately, use a key argument to max : 或者,对max使用key参数:

best = max(genes, key=lambda k: compareGenes(k, target))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM