【问题标题】:return the first item of a sublist that has the maximum value at the third column返回在第三列具有最大值的子列表的第一项
【发布时间】:2018-04-14 00:43:33
【问题描述】:

我以这个列表为例

favourite_candy_boys_girls = [['Chips' , 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4]]

第二列是男孩,第三列是女孩。我想写一个函数来返回女孩最喜欢的第一个糖果(在这种情况下应该是巧克力)。我知道我是否运行以下命令:

max(l[2] for l in favourite_candy_boys_girls)

我会找到最大值,但我不太确定如何退回糖果

编辑 我正在考虑使用这样的循环将它写成一个函数:

>>> def get_candy_with_max_girls(Candy: SystemData):
    """Return the candy type that has the most girls.
     If there is a tie of girls for a particular candy type, return the 
     candy type that appears first in candy.

>>> get_candy_with_max_girls(favourite_candy_boys_girls):
'Chocolate'
 """

【问题讨论】:

    标签: python list loops nested-loops


    【解决方案1】:

    您可以使用 max 函数的 key 参数。它允许您在对项目进行排序之前提供将用于项目的函数:

    l = [['Chips' , 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4]]
    max(l, key=lambda x: x[2])[0]  
    # returns 'Chocolate'
    

    【讨论】:

      【解决方案2】:

      如果你改用字典,键是食物,你可以使用这个代码:

      testlist = {"Chips":[1,2],"Chocolate":[3,4]}
      
      output = max(testlist, key=lambda x: testlist[x][-1])
      print output
      

      哪些输出:

      Chocolate
      

      【讨论】:

      • @DRPK 是正确的,我错过了订单对 OP 很重要的观点。打算留下代码,以便未来的读者可以避免这个错误。
      【解决方案3】:

      这将起作用:

      favourite_candy_boys_girls = [['Chips' , 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4]]
      max(favourite_candy_boys_girls, key=lambda x: x[2])[0]
      # 'Chocolate'
      

      最大呼叫通过按键功能为您提供与最大女孩的三胞胎。然后你就抓住那个三胞胎的糖果。

      【讨论】:

        【解决方案4】:

        在做max的时候用一个键

        def get_candy_with_max_girls(favourite_candy_boys_girls):
            return max(favourite_candy_boys_girls, key = lambda k:k[2])[0]
        

        【讨论】:

          【解决方案5】:

          试试这个(不需要图书馆,适用于女孩和男孩,也适用于多个最大糖果值):

          favourite_candy_boys_girls = [['Chips', 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4], ["gumdrop", 3, 4]]
          
          
          my_dict = {}
          
          # Convert Your List to Dictionary
          for candy_box in favourite_candy_boys_girls:
              my_dict[candy_box[0]] = {"Girls": candy_box[1], "Boys": candy_box[2]}
          
          
          def the_most_delicious_candy(get_dict, girl_or_boy):
          
              if girl_or_boy == "girls":
          
                  just_girls = {}
                  for candy, values in get_dict.items():
                      just_girls[candy] = values["Girls"]
          
                  find_max = max(just_girls.values())
          
                  last_buffer = []
                  for candy, values in get_dict.items():
          
                      if values['Girls'] == find_max:
          
                          make_new_dict = { candy: get_dict[candy] }
                          last_buffer.append(make_new_dict)
          
                  return last_buffer
          
              elif girl_or_boy == "boys":
          
                  just_boys = {}
                  for candy, values in get_dict.items():
                      just_boys[candy] = values["Boys"]
          
                  find_max = max(just_boys.values())
          
                  last_buffer = []
                  for candy, values in get_dict.items():
          
                      if values['Boys'] == find_max:
                          make_new_dict = {candy: get_dict[candy]}
                          last_buffer.append(make_new_dict)
          
                  return last_buffer
          
              else:
                  return "Just Girls and Boys are available"
          

          招男生:

          print(the_most_delicious_candy(my_dict, "boys"))
          

          男孩输出:

          [{'Chocolate': {'Boys': 4, 'Girls': 3}}, {'Lollipops': {'Boys': 4, 'Girls': 2}}, {'gumdrop': {'Boys': 4, 'Girls': 3}}]
          

          呼吁女孩:

          print(the_most_delicious_candy(my_dict, "girls"))
          

          女孩输出:

          [{'gumdrop': {'Boys': 4, 'Girls': 3}}, {'Chocolate': {'Boys': 4, 'Girls': 3}}]
          

          祝你好运...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-09-04
            • 1970-01-01
            • 2020-06-09
            • 1970-01-01
            • 1970-01-01
            • 2018-11-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多