You can sort the list-of-lists by the first element of each item and then groupby
the same element.
import itertools
l = [[1, 2], [5,585], [2, 0], [1, 500], [2, 668], [3, 54], [4, 28], [3, 28], [4,163], [3,85], [5,906], [2,5000], [6,358], [4,69], [3,89], [7, 258],[5, 632], [7, 585]]
l.sort(key=lambda item: item[0])
groups = { k: [item[1] for item in v]
for k, v in itertools.groupby(l, key=lambda item: item[0])}
Why do I need to sort first?
This gives groups=
{1: [2, 500],
2: [0, 668, 5000],
3: [54, 28, 85, 89],
4: [28, 163, 69],
5: [585, 906, 632],
6: [358],
7: [258, 585]}
An explanation of the groups = ...
line:
- First, I
groupby()
the sorted list using the first element of each item as the key. This groups all elements with the same key in a single iterable, so for the key of 1
, we'd have an iterable containing the elements [1, 2]
and [1, 500]
.
- I iterate over this
groupby()
result, and create a dictionary using a dict comprehension
- For the keys of the dict, I use the key of the
groupby()
- For the values of the dict, I have a list comprehension that iterates over each item in the group, and takes only the first element of that item (so the
1
key will now have a value that is a list containing 2
and 500
).
Then, find the max
and min
of each group, using only the second element of each item:
max_vals = {k: max(v) for k, v in groups.items()}
# {1: 500, 2: 5000, 3: 89, 4: 163, 5: 906, 6: 358, 7: 585}
min_vals = {k: min(v) for k, v in groups.items()}
# {1: 2, 2: 0, 3: 28, 4: 28, 5: 585, 6: 358, 7: 258}
avg_vals = {k: sum(v) / len(v) for k, v in groups.items()}
# {1: 251.0, 2: 1889.3333333333333, 3: 64.0, 4: 86.66666666666667, 5: 707.6666666666666, 6: 358.0, 7: 421.5}
Or, to print them all the way you want:
for k, v in groups.items():
print(f"Max for first element with {k}: {max(v)}")
print(f"Min for first element with {k}: {min(v)}")
print(f"Average: {sum(v) / len(v)}")
Which gives:
Max for first element with 1: 500
Min for first element with 1: 2
Average: 251.0
Max for first element with 2: 5000
Min for first element with 2: 0
Average: 1889.3333333333333
Max for first element with 3: 89
Min for first element with 3: 28
Average: 64.0
... and so on