1

我对如何制作一个计算所有变量的最小值的函数感到困惑

例如

>>>Myscore1 = 6 
>>>Myscore2 =-3
>>>Myscore3 = 10 

该函数将返回分数,如果它是最小值,则返回 True ,否则返回 False 。因此,从上面的示例中,输出将是:

>>>[(6,False),(-3,True),(10,False)]
4

3 回答 3

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)]
于 2013-05-24T11:20:15.873 回答
1

很简单:

>>> scores = [Myscore1, Myscore2, Myscore3]
>>> [(x, (x == min(scores))) for x in scores]
[(6, False), (-3, True), (10, False)]
于 2013-05-24T11:55:03.090 回答
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))]
于 2013-05-24T11:47:26.000 回答