python字典列表过滤器

字典列表过滤器

需求

需求中对 获取到的字典列表根据关键字进行过滤, 筛选符合条件的数据

  • 支持单个字段, 单个或多个数据过滤

    {
    	"name": [ "m1.large","m1.xlarge","wangjw"]
    }
    # 过滤字段 "name", 满足列表中的任意一个即可
    
  • 支持多个字段

    {
        "name":[ "m1.large","m1.xlarge","wangjw"],
        "ram": 4096
    }
    # 必须同时满足 name, ram字段 对应的value值
    

示例

  • 原始数据

    d = [
            {
                'disk': 1,
                'id': '1',
                'is_disabled': False,
                'is_public': True,
                'name': 'm1.tiny',
                'ram': 512,
                'vcpus': 1
            },
            {
                'disk': 20,
                'id': '2',
                'is_disabled': False,
                'is_public': True,
                'name': 'm1.small',
                'ram': 2048,
                'vcpus': 1
            },
            {
                'disk': 40,
                'id': '3',
                'is_disabled': False,
                'is_public': True,
                'name': 'm1.medium',
                'ram': 4096,
                'vcpus': 2
            },
            {
                'disk': 80,
                'id': '4',
                'is_disabled': False,
                'is_public': True,
                'name': 'm1.large',
                'ram': 8192,
                'vcpus': 4
            },
            {
                'disk': 160,
                'id': '5',
                'is_disabled': False,
                'is_public': True,
                'name': 'm1.xlarge',
                'ram': 16384,
                'vcpus': 8
            },
            {
                'disk': 50,
                'id': 'abb677c9-1bf2-415d-97bd-ef62574690ed',
                'is_disabled': False,
                'is_public': True,
                'name': 'wangjw',
                'ram': 4096,
                'vcpus': 2
            },
            ...
        ]
    
  • 过滤条件如下

    filters = {
        "name": ["m1.large", "m1.xlarge", "wangjw"],
        "ram": 4096
    }
    
  • 最终结果

    [
    	{
    	  'disk': 50,
          'id': 'abb677c9-1bf2-415d-97bd-ef62574690ed',
          'is_disabled': False,
          'is_public': True,
          'name': 'wangjw',
          'ram': 4096,
          'vcpus': 2
          }
    ]
    

代码

  • 代码如下

    #!/usr/bin/env python
    # ~*~ coding: utf-8 ~*~
      def list_filter(l, filters=None):
          """通过特殊字段过滤原始列表字典
          :param filters: 字典构建的过滤字段
          格式如下
          1.同一字段,匹配多个选项
          {
          "name":[ "m1.large","m1.xlarge","wangjw"]
          }
          2.混合模式多个字段,不同字段有独立的匹配项
          {
          "name":[ "m1.large","m1.xlarge","wangjw"],
          "ram": 4096
          }
          :param l: 目标字典列表
          :return: 过滤后的列表
          """
          rest_l = copy.deepcopy(l)
          if not filters:
              return l
    
          for i in rest_l:
              for k, v in filters.items():
                  if isinstance(v, (list, tuple)) and i.get(k) not in v:
                      l.remove(i)
                      break
                  elif not isinstance(v, (list, tuple)) and i.get(k) != v:
                      l.remove(i)
                      break
          return l
    
    • 使用copy 是因为字列表字典中 每个元素都是字典, 而字典属于引用性类型, 整个列表也就变成了引用性类型, 当进行remove操作时, 原始的列表也会更改
原文地址:https://www.cnblogs.com/failymao/p/12874623.html