rsev4292340
rsev4292340

Reputation: 679

Group the List of Dictionary into Class objects

I have a list of dictionary as follows with 4 keys

dict_list = [
{'key1': 'a', 'key2':'b' , 'key3':'p' , 'key4':'q' },
{'key1': 'a', 'key2':'b' , 'key3':'p' , 'key4':'r' },
{'key1': 'a', 'key2':'c' , 'key3':'p' , 'key4':'q' },
{'key1': 'a', 'key2':'c' , 'key3':'p' , 'key4':'r' },
{'key1': 'a', 'key2':'d' , 'key3':'p' , 'key4':'q' }
]

I have a class abc as follows

class abc:
   def __init__(self, val1, val2, val3, val4)
       self.val1 = val1
       self.val2 = val2
       self.val3 = val3
       self.val4 = val4

@classmethod
def from_resultdict(cls, result):
        return cls(result['key1'],
                   result['key2'],
                   result['key3'],
                   result['key4'])

I get the List[abc] with the following snippet

return [abc.from_resultdict(r) for r in dict_list]

I now like to add a class2 for grouping of the list of objects based on values of key1 and key2

class class2:
      def __init__(self, value1, value2)
          self.value1 = value1
          self.value2 = value2
          self.list_of_abcs: List[abc]

I want to have 3 objects of class2 for the dict_list such as

object1 {
  value1 = a
  value2 = b
  list_of_abcs = [abc_object1 , abc_object2]
}

object2 {
 value1 = a
 value2 = c
 list_of_abcs = [abc_object3 , abc_object4]
}

object3 {
 value1 = a
 value2 = d
 list_of_abcs = [abc_object5]
}

How can i achieve this?

Upvotes: 0

Views: 1558

Answers (2)

Wups
Wups

Reputation: 2569

Create a dict for the Class2-objects so you can find the correct one for each Abc-object.

class Class2:
    def __init__(self, value1, value2)
        self.value1 = value1
        self.value2 = value2
        self.list_of_abcs = []
    
objects = {}

for abc in abc_list:
    key = (abc.val1, abc.val2)
    if key not in objects: # create a Class2 instance if it doesn't exist yet
        objects[key] = Class2(key[0], key[1])
    # append the abc-object to the correct Class2 instance
    objects[key].list_of_abcs.append(abc)

Upvotes: 1

AKX
AKX

Reputation: 168967

Let's just ignore the classes for now; you can get your grouping with our good friend collections.defaultdict:

import collections
from pprint import pprint

dict_list = [
    {'key1': 'a', 'key2': 'b', 'key3': 'p', 'key4': 'q'},
    {'key1': 'a', 'key2': 'b', 'key3': 'p', 'key4': 'r'},
    {'key1': 'a', 'key2': 'c', 'key3': 'p', 'key4': 'q'},
    {'key1': 'a', 'key2': 'c', 'key3': 'p', 'key4': 'r'},
    {'key1': 'a', 'key2': 'd', 'key3': 'p', 'key4': 'q'}
]

groups = collections.defaultdict(list)
for d in dict_list:
    groups[(d['key1'], d['key2'])].append(d)

pprint(dict(groups))

outputs

{('a', 'b'): [{'key1': 'a', 'key2': 'b', 'key3': 'p', 'key4': 'q'},
              {'key1': 'a', 'key2': 'b', 'key3': 'p', 'key4': 'r'}],
 ('a', 'c'): [{'key1': 'a', 'key2': 'c', 'key3': 'p', 'key4': 'q'},
              {'key1': 'a', 'key2': 'c', 'key3': 'p', 'key4': 'r'}],
 ('a', 'd'): [{'key1': 'a', 'key2': 'd', 'key3': 'p', 'key4': 'q'}]}

From there you can wrap the output (for group_key, members in groups.items()) into classes/objects as you like.

Upvotes: 1

Related Questions