【问题标题】:Return the dictionary from multiple lists of dictionaries with the highest value in a specific key从多个字典列表中返回特定键中具有最高值的字典
【发布时间】:2020-05-16 23:38:40
【问题描述】:

我有以下两个字典列表:

    regionA = [dict(is_buy_order=True, price=1000, type=1),
               dict(is_buy_order=True, price=100, type=2),
               dict(is_buy_order=False, price=10, type=2)]
    regionB = [dict(is_buy_order=False, price=10, type=1),
               dict(is_buy_order=True, price=100, type=1),
               dict(is_buy_order=True, price=1000, type=2)]

考虑到来自 regionA 和 regionB 的数据,我想返回类型 1 和类型 2 的键 'price' 中具有最高值和键 'is_buy_order' 中的值 'True' 的字典.

我见过一些例子(例如Return the dictionary from a list of dictionaries with the highest value in a specific key)适用于单个字典列表,但不能使它们与集合一起使用,我想知道是否有直接的方法。

(这里的第一个问题:如果需要澄清,请告诉我,感谢您的帮助!)

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    你可以这样做:

    both_regions = regionA + regionB
    result = sorted([d for d in both_regions if d['is_buy_order']],
                    key=lambda x: x['price'], reverse=True)[0]
    

    输出

    {'is_buy_order': True, 'price': 1000, 'type': 1}
    

    【讨论】:

      【解决方案2】:

      您可以将字典集转换为 pandas 数据框,这将使您的生活更加轻松:(这不是完整的代码,但遵循这些原则......)

      import pandas as pd
      Region = dict(regionA,regionB)
      df = pd.DataFrame(Region, columns= ['Region','BuyOrder', 'Price','Type'])
      df.sort_values(by=['Price'], inplace=True)
      

      【讨论】:

        【解决方案3】:
        regionA = [dict(is_buy_order=True, price=1000, type=1),
          dict(is_buy_order=True, price=100, type=2),
          dict(is_buy_order=False, price=10, type=2)]
        
        regionB = [dict(is_buy_order=False, price=10, type=1),
                       dict(is_buy_order=True, price=100, type=1),
                       dict(is_buy_order=True, price=1000, type=2)]
        
        c = regionA + regionB
        type1 = dict(price=0)
        type2 = dict(price=0)
        for item in c:
          if (type1["price"]  < item["price"]) and item["is_buy_order"] and item["type"]==1:
            type1 = item
          if (type2["price"] < item["price"]) and item["is_buy_order"] and item["type"]==2:
            type2 = item
        
        print('type1 max price', type1)
        print('type2 max price', type2)
        

        【讨论】:

          【解决方案4】:

          您可以通过 regionC = regionA + regionB 连接字典。

          然后,您可以应用以下内容:

           type_1 = max((x for x in regionC if x['is_buy_order'] and x['type'] == 1), key=lambda x: x['price'])
          print(type_1)
           type_2 = max((x for x in regionC if x['is_buy_order'] and x['type'] == 2), key=lambda x: x['price'])
          print(type_2)
          

          输出将是:

          {'is_buy_order': True, 'price': 1000, 'type': 1}
          {'is_buy_order': True, 'price': 1000, 'type': 2}
          
          

          如果您不想考虑type,您可以将其从列表理解中删除。 然后就是

           type_1 = max((x for x in regionC if x['is_buy_order']), key=lambda x: x['price'])
          

          【讨论】:

            【解决方案5】:

            这应该可行。首先,我将列表regionAregionB 连接成一个列表total_data。然后对于每种类型,我从满足is_buy_order 为真且type 为相应类型的约束的所有订单中找出最高价格。

            total_data = regionA + regionB    
            maxType1 = max([order['price'] for order in total_data if order['is_buy_order'] and order['type'] == 1])
            maxType2 = max([order['price'] for order in total_data if order['is_buy_order'] and order['type'] == 2])
            

            【讨论】:

              【解决方案6】:

              您可以先加入列表:

              max([d for d in regionA + regionB if  d['is_buy_order']], key=lambda d: d['price']))
              

              【讨论】:

                猜你喜欢
                • 2019-11-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多