我对如何制作一个计算所有变量
的最小值的函数感到困惑例如
>>>Myscore1 = 6
>>>Myscore2 =-3
>>>Myscore3 = 10
该函数将返回分数,如果是最小值,则返回True,否则返回False。 因此,从上面的例子中,输出将是:
>>>[(6,False),(-3,True),(10,False)]
答案 0 :(得分:4)
scores = [6, -3, 10]
def F(scores):
min_score = min(scores)
return [(x, x == min_score) for x in scores]
>>> F(scores)
[(6, False), (-3, True), (10, False)]
答案 1 :(得分:1)
使用enumerate
scores = [6, -3, 10]
import operator
res = [[scores[i], True]
if i == min(enumerate(scores), key = operator.itemgetter(1))[0]
else [scores[i], False]
for i in range(len(scores))]
答案 2 :(得分:1)
很简单:
>>> scores = [Myscore1, Myscore2, Myscore3]
>>> [(x, (x == min(scores))) for x in scores]
[(6, False), (-3, True), (10, False)]