【问题标题】:Picking largest number form a list not working [closed]从列表中选择最大的数字不起作用[关闭]
【发布时间】:2015-04-20 08:09:51
【问题描述】:

我目前正在尝试制作一个包含分数列表及其标题的模块,并尝试从列表中找到得分最高的一个。我的代码有时似乎可以工作,而有时却不行。这只是 python 2 中的一个简单的比较模块,但我无法让它工作。

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

实际输出:“else none”

预期输出: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

【问题讨论】:

  • 能否请您发布一些具有预期和实际输出的示例?
  • 我认为similarity_percent_lst 是输入,标题和分数是交替的。我可以建议不要将所有输入存储在一个混合数据类型的数组中,而是拥有一个元组(或对象)列表,其中每个元组都包含其相关数据。 input = [("who_is_cmds", 50.0), ("when_is_cmds", 15.0)]
  • 你的代码永远不会像这样工作,因为你没有缩进你的 for 循环。
  • 您是否要查找与最高数字关联的字符串?
  • 是的,我正在尝试查看哪个具有最高值并获取与之关联的字符串@StephenPaulger

标签: python list python-2.7 sorting


【解决方案1】:
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 可以轻松地比较项目。

【讨论】:

    【解决方案2】:

    此解决方案大致基于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. 列表中的元素按 2×2 分组:zip(spcl,spcl)

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

    用于配对可迭代项中的项目的成语zip(it,it)导致grouper配方。
    我将您转至第一段中的参考资料,以便对其进行完整讨论。

    【讨论】:

    • 每个人都应该在某个时候了解这个食谱是如何工作的(一两年前有人问过这个问题,这个问题被关闭得太宽泛了,所以I wrote a whole blog post about it...)。但是我认为OP现在不会将精力投入到学习中,因此切片答案可能会更好。 (我都赞成。)
    • 你提出的问题(OP 不去......)是一个重要的问题,当我看到未来需要时,我会尝试添加更简单的答案,Tx。跨度>
    • StephenPaulger 已经添加了另一个,这里绝对值得拥有。 (即使 OP 没有得到你的答案,毕竟有同样问题的其他人可能会。)
    【解决方案3】:

    依靠标准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')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      相关资源
      最近更新 更多