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?
vals from 1 to 3 and your dict has keys from 1 to 4. What would you do withkey4?key4: ...?