Grouping a list of dictionaries by the same value in Python 3

Given a list of dictionaries:

players= [
   { "name": 'matt', 'school': 'WSU', 'homestate': 'CT', 'position': 'RB' },
   { "name": 'jack', 'school': 'ASU', 'homestate': 'AL', 'position': 'QB' },
   { "name": 'john', 'school': 'WSU', 'homestate': 'MD', 'position': 'LB' },
   { "name": 'kevin', 'school': 'ALU', 'homestate': 'PA', 'position': 'LB' },
   { "name": 'brady', 'school': 'UM', 'homestate': 'CA', 'position': 'QB' },
]

How can I group them into groups by matching their corresponding dictionary values ​​so that they erupt:

Matching value 1:

name: [matt, john, kevin],

     

School: [WSU, WSU, ALU],

     

homestate: [CT, MD, PA]

     

position: [RB, LB, LB]

Matching value 2:

name: [jack, brady],

     

School: [ASU, UM],

     

homestate: [AL, CA]

     

position: [QB, QB]

Note that matching values ​​are arbitrary; that is, it can be found anywhere. Maybe it's in schoolor in position, or maybe in both.

I tried to group them by doing:

from collections import defaultdict

result_dictionary = {}

for i in players:
    for key, value in i.items():
        result_dictionary.setdefault(key, []).append(value)

What issues:

{'name': ['matt', 'jack', 'john', 'kevin', 'brady'], 
 'school': ['WSU', 'ASU', 'WSU', 'ALU', 'UM'], 
 'homestate': ['CT', 'AL', 'MD', 'PA', 'CA'], 
 'position': ['RB', 'QB', 'LB', 'QB', 'QB']}

, , , , , .

+4
2

collections.defaultdict, :

In [21]: from collections import defaultdict
    ...: result = defaultdict(lambda: defaultdict(list))
    ...: for d in players:
    ...:     for k,v in d.items():
    ...:         result[d['school']][k].append(v)
    ...:

In [22]: result
Out[22]:
defaultdict(<function __main__.<lambda>>,
            {'ASU': defaultdict(list,
                         {'homestate': ['AL'],
                          'name': ['jack'],
                          'position': ['QB'],
                          'school': ['ASU']}),
             'WSU': defaultdict(list,
                         {'homestate': ['CT', 'MD'],
                          'name': ['matt', 'john'],
                          'position': ['RB', 'LB'],
                          'school': ['WSU', 'WSU']})})
+3

:

import itertools
players= [
  { "name": 'matt', 'school': 'WSU', 'homestate': 'CT', 'position': 'RB' },
  { "name": 'jack', 'school': 'ASU', 'homestate': 'AL', 'position': 'QB' },
  { "name": 'john', 'school': 'WSU', 'homestate': 'MD', 'position': 'LB' },
  { "name": 'kevin', 'school': 'ALU', 'homestate': 'PA', 'position': 'S' },
  { "name": 'brady', 'school': 'UM', 'homestate': 'CA', 'position': 'QB' },
]
headers = ['name', 'school', 'homestate', 'position'] 
final_header = [[a, max(b, key=lambda x:b.count(x))] for a, b in zip(headers, zip(*[[i[b] for b in headers] for i in players])) if len(set(b)) < len(b)]
d = [[list(b) for _, b in itertools.groupby(filter(lambda x:x[i] == c, players), key=lambda x:x[i])][0] for i, c in final_header]
last_results = {'pattern {}'.format(i):{d[0][0]:[j[-1] for j in d] for c, d in zip(headers, zip(*map(dict.items, h)))} for i, h in enumerate(d, start=1)}

:

{'pattern 2': 
  {'homestate': ['AL', 'CA'], 
  'school': ['ASU', 'UM'], 
  'name': ['jack', 'brady'], 
  'position': ['QB', 'QB']}, 

'pattern 1': 
    {'homestate': ['CT', 'MD'], 
    'school': ['WSU', 'WSU'], 
    'name': ['matt', 'john'], 
    'position': ['RB', 'LB']}
}
+1

Source: https://habr.com/ru/post/1695140/


All Articles