python dictionary list filter

Keywords: Python

catalog

Dictionary list filter

demand

In the requirements, the dictionary list obtained is filtered according to the keywords, and the qualified data is filtered

  • Support single field, single or multiple data filtering

    {
    	"name": [ "m1.large","m1.xlarge","wangjw"]
    }
    # Filter the field "name" to satisfy any one in the list
    
  • Multiple fields supported

    {
        "name":[ "m1.large","m1.xlarge","wangjw"],
        "ram": 4096
    }
    # The value value corresponding to the name and ram fields must be met at the same time
    

Example

  • raw data

    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
            },
            ...
        ]
    
  • The filter conditions are as follows

    filters = {
        "name": ["m1.large", "m1.xlarge", "wangjw"],
        "ram": 4096
    }
    
  • final result

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

code

  • The code is as follows

    #!/usr/bin/env python
    # ~*~ coding: utf-8 ~*~
      def list_filter(l, filters=None):
          """Filter the original list dictionary with special fields
          :param filters: Filter fields for dictionary construction
          //The format is as follows
          1.Same field,Match multiple options
          {
          "name":[ "m1.large","m1.xlarge","wangjw"]
          }
          2.Mixed mode multiple fields,Different fields have independent matches
          {
          "name":[ "m1.large","m1.xlarge","wangjw"],
          "ram": 4096
          }
          :param l: Target dictionary list
          :return: Filtered list
          """
          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 is used because every element in the word list dictionary is a dictionary, and the dictionary belongs to the referential type, so the whole list becomes a referential type. When you remove, the original list will also change

Posted by byronbailey on Tue, 12 May 2020 08:03:39 -0700