I have the following list:
l = [(('01001', '01003'), 4.15),
(('01001', '01005'), 2.83),
(('01001', '01007'), 3.32),
(('01001', '01008'), 2.32),
(('01001', '01009'), 9.32),
(('01001', '01007'), 0.32),
(('01002', '01009'), 6.83),
(('01002', '01011'), 2.53),
(('01002', '01009'), 6.83),
(('01002', '01011'), 2.53),
(('01002', '01009'), 6.83),
(('01002', '01011'), 2.53),
(('01003', '01013'), 20.50),
(('01003', '01013'), 10.50),
(('01003', '01013'), 0.50),
(('01003', '01013'), 2.50),
(('01003', '01013'), 20.30),
(('01003', '01013'), 12.50),
(('01003', '01013'), 1.50),
(('01003', '01013'), 2.40)]
I would like to select the n-smallest values for the first element of this list ('01001', '01002', and '01003').
I was able to calcualte the min value with this code:
from itertools import groupby
from statistics import mean
{k:min(v for *_, v in v) for k,v in groupby(result_map, lambda x: x[0][0])}
but would like to get the 3 smallest values and the second column to be printed:
Expected outcome would be a dictionary like this:
{'01001': ['01007', '01008', '01005'], '01002': ['01011', '01009', '01013'] , '01003': ['01013', '01013', ''01013']}
Any help would be much appreciated!