Need your help, to solve strugle with list of lists filtering, merging and summarizing. My input params:
- Nested list structure [p0-n, p1-n, p2-n, q3, q4, q5] n - num
- Items using for filtering and merging (p0 and p1 and p2)
- Summarize (q3, q4, q5) if all three control items (p0, p1, p2) in looping sublist are the same. And merge to one common nested list.
Sample list (in_list):
in_list = [
['p0-1', 'p1-1', 'p2-1', 1, 1, 2],
['p0-1', 'p1-1', 'p2-1', 2, 1, 1],
['p0-1', 'p1-1', 'p2-1', 1, 2, 1],
['p0-2', 'p1-1', 'p2-1', 1, 1, 1],
['p0-2', 'p1-2', 'p2-1', 1, 1, 1],
['p0-2', 'p1-1', 'p2-2', 1, 1, 1]
]
My variant (I found it here) what i've got for now:
out_list = [[k, sum(q1 for p1, p2, p3, q1, q2, q3 in g)]
for k, g in groupby(sorted(in_list), key=itemgetter(0, 1, 2))]
My result (out_list):
print(out_list)
[
[('p0-1', 'p1-1', 'p2-1'), 4],
[('p0-2', 'p1-1', 'p2-1'), 1],
[('p0-2', 'p1-1', 'p2-2'), 1],
[('p0-2', 'p1-2', 'p2-1'), 1]
]
This comprehension almost do what I need, but summarizing only one q1 (or q2 or q3). And my tries about insert q2, q3 to this comprehension was unsuccessful. Is it possible to modify this comprehension to summarize q1, q2 and q3 at the same time? Or maybe exist more simple way to achieve what I need?
Desirable result list (out_list):
[
['p0-1', 'p1-1', 'p2-1', 4, 4, 4],
['p0-2', 'p1-1', 'p2-1', 1, 1, 1],
['p0-2', 'p1-2', 'p2-1', 1, 1, 1],
['p0-2', 'p1-1', 'p2-2', 1, 1, 1]
]
Thanks in advance and appreciate for any useful advises and help.