I have the following list:
mylist: [[(5, 1, 11), (5, 2, 13), (5, 3, 26),
(3, 1, 60), (3, 2, 40), (3, 3, 70),
(6, 1, 30), (6, 2, 80), (2, 3, 80)],
[(5, 1, 7), (5, 2, 8), (5, 3, 6),
(3, 1, 50), (3, 2, 44), (3, 3, 44),
(6, 1, 20), (6, 2, 40), (2, 3, 50)],
[(5, 1, 22), (5, 2, 18), (5, 3, 60),
(3, 1, 10), (3, 2, 20), (3, 3, 30),
(6, 1, 60), (6, 2, 20), (2, 3, 30)]]
I want to calculate the average of the items which have the same “first and the second elements”. E.g., from the below example, I want to take the average of the elements which have ‘5’ and ‘1’ in the first two elements of the list. So, my desired output should be like this:
output: [(5, 1, 13.3), (5, 2, 25.6), (5, 3, 30.6),
(3, 1, 40), (3, 2, 34.6), (3, 3, 48),
(6, 1, 36.6), (6, 2, 46.6), (6, 3, 56.6)]
If I have only two items in the lists like:
mylist: [[(1, 11), ( 2, 13), ( 3, 26),
[( 1, 60), ( 2, 40), ( 3, 70)],...]
I could easily calculate the average by the below code:
np.mean(mylist, axis=0)
Advertisement
Answer
see below
from collections import defaultdict
lst = [[(5, 1, 11), (5, 2, 13), (5, 3, 26),
(3, 1, 60), (3, 2, 40), (3, 3, 70),
(6, 1, 30), (6, 2, 80), (2, 3, 80)],
[(5, 1, 7), (5, 2, 8), (5, 3, 6),
(3, 1, 50), (3, 2, 44), (3, 3, 44),
(6, 1, 20), (6, 2, 40), (2, 3, 50)],
[(5, 1, 22), (5, 2, 18), (5, 3, 60),
(3, 1, 10), (3, 2, 20), (3, 3, 30),
(6, 1, 60), (6, 2, 20), (2, 3, 30)]]
data = defaultdict(list)
for ex_entry in lst:
for in_entry in ex_entry:
data[(in_entry[0], in_entry[1])].append(in_entry[2])
for key, value in data.items():
print(f'{key} -> {sum(value) / len(value)}')
output
(5, 1) -> 13.333333333333334 (5, 2) -> 13.0 (5, 3) -> 30.666666666666668 (3, 1) -> 40.0 (3, 2) -> 34.666666666666664 (3, 3) -> 48.0 (6, 1) -> 36.666666666666664 (6, 2) -> 46.666666666666664 (2, 3) -> 53.333333333333336