从列表中选取最大数字不起作用

时间:2015-04-20 08:09:51

标签: python list python-2.7 sorting

我目前正在尝试制作一个模块,其中包含一系列分数和标题,并试图从列表中找到最大的得分。我有的代码似乎工作了一段时间,而有时却没有。它只是python 2中的一个简单的比较模块,但我无法让它工作。

EDITED: 所以基本上输出应该是命令sec的格式,以及列表中最高的数字。但实际输出是“否则无”,如果整个列表没有增加或减少,则会弹出。

实际输出:“否则无”

预期输出:what_is_cmds 100.0

similarity_percent_lst = ["who_is_cmds", "50.0", "when_is_cmds", "15.0","what_is_cmds", "100.0", "where_is_cmds", "50.0", "personal_info_cmds", "33.33333333333333", "personal_emotion_cmds", "20.0", "personal_emotion_cmds", "40.0", "bye_cmds", "33.33333333333333", "time_cmds", "100.0", "time_cmds", "60.0", "time_cmds", "75.0", "time_cmds", "50.0", "time_cmds", "50.0", "time_cmds", "100.0", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "50.0", "date_cmds", "40.0", "date_cmds", "66.66666666666666", "date_cmds", "66.66666666666666", "date_cmds", "50.0", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "weather_cmds", "50.0", "weather_cmds", "66.66666666666666", "weather_cmds", "33.33333333333333"]

biggest_percent_similarity = 50.0
biggest_percent_cmd_sec = "who_is_cmds"
# GETS THE BIGGEST OUT OF ALL THE ONES THAT WERE SIMILAR AND MAKES THAT THE FINAL CHOSEN COMMAND.
if len(similarity_percent_lst) > 2:
    for each_number in range(3, len(similarity_percent_lst),2):

        # if the n command in the similarity list is more than the n-1 command in the similarity list then this executes.
        if (similarity_percent_lst[each_number] > biggest_percent_similarity) == True:
            print "\n" + "similarity_percent_lst[each_number] > biggest_percent_similarity"
            print similarity_percent_lst[each_number] + ">" + str(biggest_percent_similarity)
            print "biggest_percent_similarity BEFORE: ",biggest_percent_similarity,str(each_number)
            biggest_percent_similarity = float(similarity_percent_lst[each_number])

            biggest_percent_cmd_sec = similarity_percent_lst[each_number - 1]

            print "biggest_percent_similarity NOW: ",biggest_percent_similarity,str(each_number)

            score = biggest_percent_cmd_sec + " " + str(biggest_percent_similarity)

        # if the n command in the similarity list is less than the n-1 command in the similarity list then this executes.
        elif (similarity_percent_lst[each_number] < biggest_percent_similarity) == True:
            print "\n" + "similarity_percent_lst[each_number] < biggest_percent_similarity"
            print similarity_percent_lst[each_number] + ">" + str(biggest_percent_similarity)
            print "biggest_percent_similarity BEFORE: ",biggest_percent_similarity,str(each_number)
            biggest_percent_similarity = similarity_percent_lst[each_number - 2]

            biggest_percent_cmd_sec = similarity_percent_lst[each_number - 3]

            print "biggest_percent_similarity: ",biggest_percent_similarity,str(each_number)

            score = biggest_percent_cmd_sec + " " + str(biggest_percent_similarity)

        else:
            score = "\n" + "else none"

elif len(similarity_percent_lst) < 2:
    score = "\n" + "elif none"

else:
    print "\n" + "it aint two numbers"
        biggest_percent_cmd_sec = similarity_percent_lst[0]

    biggest_percent_similarity = similarity_percent_lst[1]

    score = biggest_percent_cmd_sec + " " + str(biggest_percent_similarity),str(each_number)

print similarity_percent_lst
print "score: " + score

3 个答案:

答案 0 :(得分:3)

orig_input = ["who_is_cmds", "50.0", "when_is_cmds", "15.0","what_is_cmds", "100.0", "where_is_cmds", "50.0", "personal_info_cmds", "33.33333333333333", "personal_emotion_cmds", "20.0", "personal_emotion_cmds", "40.0", "bye_cmds", "33.33333333333333", "time_cmds", "100.0", "time_cmds", "60.0", "time_cmds", "75.0", "time_cmds", "50.0", "time_cmds", "50.0", "time_cmds", "100.0", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "50.0", "date_cmds", "40.0", "date_cmds", "66.66666666666666", "date_cmds", "66.66666666666666", "date_cmds", "50.0", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "weather_cmds", "50.0", "weather_cmds", "66.66666666666666", "weather_cmds", "33.33333333333333"]

cleaned_input = [(orig_input[i], float(orig_input[i+1])) 
                 for i in range(0, len(orig_input), 2)]

print max(cleaned_input, key=lambda x: x[1])

orig_input与您的similarity_percent_lst相同。首先,我们将该数组解析为2元组列表。单个2元组可能看起来像("when_is_cmds", 15.0)。我们还将得分的字符串转换为浮点数。

然后我们使用max,向其传递一个返回数字的函数,以便max可以轻松地比较这些项目。

答案 1 :(得分:1)

此解决方案基于grouper recipethis abarnert's blog post

中的第一个示例
spcl = iter(similarity_percent_lst)
max_name, max_value = max(zip(spcl,spcl), key=lambda tpl:float(tpl[1]))
  1. 我们在列表similarity_percent_lst

  2. 上创建一个迭代器
  3. 列表中的元素按2:2分组:zip(spcl,spcl)

  4. 使用max builtin的可选key参数找到最大值(记住要从字符串转换为浮点数...)。

  5. 在迭代中配对项目的惯用语zip(it,it)会导致grouper食谱。
    我将您转到第一段中的参考资料,以便对其进行全面讨论。

答案 2 :(得分:0)

这里有一个依赖于标准python库max函数的问题的解决方案。

similarity_percent_lst = ["who_is_cmds", "50.0", "when_is_cmds", "15.0","what_is_cmds", "100.0", "where_is_cmds", "50.0", "personal_info_cmds", "33.33333333333333", "personal_emotion_cmds", "20.0", "personal_emotion_cmds", "40.0", "bye_cmds", "33.33333333333333", "time_cmds", "100.0", "time_cmds", "60.0", "time_cmds", "75.0", "time_cmds", "50.0", "time_cmds", "50.0", "time_cmds", "100.0", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "33.33333333333333", "time_cmds", "50.0", "date_cmds", "40.0", "date_cmds", "66.66666666666666", "date_cmds", "66.66666666666666", "date_cmds", "50.0", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "date_cmds", "33.33333333333333", "weather_cmds", "50.0", "weather_cmds", "66.66666666666666", "weather_cmds", "33.33333333333333"]

def fromList2tuple(listOfPairs):
    """ Take a list that contains pair of element and return them in tuples [a,b,c,d]-> [(a,b), (c,d)] """
    result = []
    for i in range(0, len(listOfPairs)-1, 2):
        result.append((listOfPairs[i], listOfPairs[i+1]))

    return result

def biggerTuple(listOfTuple, index):
    """ 
    Returns the bigger tuple of a list of tuple considering the order is determined by the element whose the index is given.
    [('a', 1), ('b', 2), ('c', -2)] , 0 ->  ('c', -2)
    [('a', 1), ('b', 2), ('c', -2)] , 1 ->  ('b',  2)
    """
    #define a function that return the element at the index of the tuple.
    orderingFunction = lambda x: x[index]
    #then use ti to order the results
    return max(listOfTuple, key=orderingFunction)

if __name__ == "__main__":

    tuples = fromList2tuple(similarity_percent_lst)
    print(biggerTuple(tuples, 1))

执行此代码我得到以下结果:('time_cmds', '75.0')