简体   繁体   中英

comparing and removal of key value data inside python list dict

I have below list dictionary data, where I am trying to iterate and compare the value of n , r , sd if all three are same then delete the entire dict block where v is lower in value.

ab = [
      {
        'n': 'abc',
        'r': 'PHX',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1627298805136,
        'v': 2,
        'o': 'symrxt7mhzuu3o5kq'
      },
      {
        'n': 'abc',
        'r': 'PHX',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1631868530689,
        'v': 3,
        'o': '52cf7qrfdalpa'
      },
      {
        'n': 'def',
        'r': 'ASHBURN',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1628827511212,
        'v': 2,
        'o': 'mjmbw2oabhxiq'
      },
      {
        'n': 'def-kup',
        'r': 'ASHBURN',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1598527021488,
        'v': 1,
        'o': '5a'
      },
      {
        'n': 'ghi',
        'r': 'AP_SYDNEY_1',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1610377812778,
        'v': 1,
        'o': '2zy'
      },
      {
        'n': 'ghi',
        'r': 'AP_SYDNEY_1',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1631877819065,
        'v': 2,
        'o': 'ongu7be56y7cc'
      },
      {
        'n': 'jkl',
        'r': 'EU_FRANKFURT_1',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1611334435645,
        'v': 1,
        'o': 'xpa'
      },
      {
        'n': 'jkl',
        'r': 'EU_FRANKFURT_1',
        'sd': 'cert',
        'dd': null,
        'TimeCreated': 1631879049498,
        'v': 2,
        'o': '57nrzqhrmwa'
      }  
    ]

I know we can compare two dicts but comparison within list of dicts for key values and deleting them I am not sure how to achieve any help will be great desired O/p:

[
  {
    'n': 'abc',
    'r': 'PHX',
    'sd': 'cert',
    'dd': null,
    'TimeCreated': 1631868530689,
    'v': 3,
    'o': '52cf7qrfdalpa'
  },
  {
    'n': 'def',
    'r': 'ASHBURN',
    'sd': 'cert',
    'dd': null,
    'TimeCreated': 1628827511212,
    'v': 2,
    'o': 'mjmbw2oabhxiq'
  },
  {
    'n': 'def-kup',
    'r': 'ASHBURN',
    'sd': 'cert',
    'dd': null,
    'TimeCreated': 1598527021488,
    'v': 1,
    'o': '5a'
  },
  {
    'n': 'ghi',
    'r': 'AP_SYDNEY_1',
    'sd': 'cert',
    'dd': null,
    'TimeCreated': 1631877819065,
    'v': 2,
    'o': 'ongu7be56y7cc'
  },
  {
    'n': 'jkl',
    'r': 'EU_FRANKFURT_1',
    'sd': 'cert',
    'dd': null,
    'TimeCreated': 1631879049498,
    'v': 2,
    'o': '57nrzqhrmwa'
  }  
]

You can use itertools.groupby ; this (i) rearranges the dicts based on n , r , sd , and (negative) v , and then (ii) takes the first dict in each group having the same n , r , and sd :

from itertools import groupby

# ab = [ ... ]

ab_sorted = sorted(ab, key=lambda d: (d['n'], d['r'], d['sd'], -d['v']))
grouping_key = lambda d: (d['n'], d['r'], d['sd'])
groups = groupby(ab_sorted, key=grouping_key)

output = [next(g) for _, g in groups]
print(output)

Output:

[
  {'n': 'abc', 'r': 'PHX', 'sd': 'cert', 'dd': 'null', 'TimeCreated': 1631868530689, 'v': 3, 'o': '52cf7qrfdalpa'},
  {'n': 'def', 'r': 'ASHBURN', 'sd': 'cert', 'dd': 'null', 'TimeCreated': 1628827511212, 'v': 2, 'o': 'mjmbw2oabhxiq'},
  {'n': 'def-kup', 'r': 'ASHBURN', 'sd': 'cert', 'dd': 'null', 'TimeCreated': 1598527021488, 'v': 1, 'o': '5a'},
  {'n': 'ghi', 'r': 'AP_SYDNEY_1', 'sd': 'cert', 'dd': 'null', 'TimeCreated': 1631877819065, 'v': 2, 'o': 'ongu7be56y7cc'},
  {'n': 'jkl', 'r': 'EU_FRANKFURT_1', 'sd': 'cert', 'dd': 'null', 'TimeCreated': 1631879049498, 'v': 2, 'o': '57nrzqhrmwa'}
]

Or using pandas with the same logic:

output = pd.DataFrame(ab).sort_values(by=['n', 'r', 'sd', 'v'], ascending=[True, True, True, False]).groupby(by=['n', 'r', 'sd'], as_index=False).first().to_dict(orient='records')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM