Skip to main content
 首页 » 编程设计

Python优雅地排序字典数组

2024年12月31日8java哥

我有这种格式的数据 -

[ 
    {'gstin_code': 'A',  
     'price_effective': 1199.0,  
     'company_id': 489,  
     'month': datetime.datetime(2018, 6, 1, 0, 0),  
     'year': datetime.datetime(2018, 1, 1, 0, 0) 
     }, 
    {'gstin_code': 'B',  
     'price_effective': 1199.0,  
     'company_id': 489,  
     'month': datetime.datetime(2018, 6, 1, 0, 0),  
     'year': datetime.datetime(2018, 1, 1, 0, 0) 
     }, 
    {'gstin_code': 'C',  
     'price_effective': 1199.0,  
     'company_id': 489,  
     'month': datetime.datetime(2018, 6, 1, 0, 0),  
     'year': datetime.datetime(2018, 1, 1, 0, 0) 
     } 
] 

预期的输出是这样的——

{ 
    "2": { 
        "2018": { 
            "3": { 
                "27AA": 1799 
            }, 
            "4": { 
                "03AA": 1299, 
                "04AA": 1499, 
                "05AA": 699, 
                "06AA": 599, 
                "07AA": 199, 
                "09AA": 499, 
                "19AA": 599, 
                "23AA": 770, 
                "27AA": 420, 
                "27AA": 499 
            }, 
            "5": { 
                "03AA": 1399, 
                "27AA": 399, 
                "27AA": 640 
            } 
         } 
     } 
  } 

即 {company_id:{year:{month:{gst:price_effective}}}}

这些值也为 None,例如 company_id、year、month、gst 确实为 None。

有效的解决方案是这个 -

for f_bag in data: 
    if f_bag['company_id'] in result and f_bag['company_id'] is not None: 
        if f_bag['year'].year in result[f_bag['company_id']] and f_bag['year'].year is not None: 
            if f_bag['month'].month in result[f_bag['company_id']][f_bag['year'].year] and f_bag['month'].month is not None: 
                if f_bag['gstin_code'] in result[f_bag['company_id']][f_bag['year'].year][f_bag['month'].month] and f_bag['gstin_code'] is not None: 
                    pass 
                else: 
                    result[f_bag['company_id']][f_bag['year'].year][f_bag['month'].month][f_bag['gstin_code']] = f_bag['price_effective'] 
            else: 
                result[f_bag['company_id']][f_bag['year'].year][f_bag['month'].month] = {} 
        else: 
            result[f_bag['company_id']][f_bag['year'].year] = {} 
    else: 
        result[f_bag['company_id']] = {} 

有什么优雅的方法可以避免这些 if else 语句?

请您参考如下方法:

使用 collections.defaultdict 你可以删除字典中是否存在键的检查:

from collections import defaultdict 
def nested_dict(): 
    return defaultdict(nested_dict) 
 
def toNested(data): 
    nd = nested_dict() 
    for aa in data:     
        nd[aa['company_id']][aa['year'].year][aa['month'].month][aa['gstin_code']] = aa['price_effective'] 
    return nd 
 
import pprint 
pp = pprint.PrettyPrinter(indent=4) 
pp.pprint( toNested(data) )