Python:具有最长键的唯一值

Python: unique values with longest keys

所以,我有一本有点像这样的字典:

'/a' : [10, 22, 12]
'/a/b' : [10, 22, 12]
'/a/b/c' : [10, 22, 12]
'/a/b/c/d' : [10, 22, 12]
'/e' : [11, 28, 16]
'/e/f' : [11, 28, 16]
'/e/f/h' : [11, 28, 16]
'/e/f/h/i' : [11, 28, 16]
'/j' : [10, 22, 12]
'/j/k' : [10, 22, 12]
'/j/k/l' : [10, 22, 12]
'/j/k/l/m' : [10, 22, 12]

我想要的是:

'/a/b/c/d' : [10, 22, 12]
'/e/f/h/i' : [11, 28, 16]
'/j/k/l/m' : [10, 22, 12]

我该怎么做?

from itertools import groupby
from operator import itemgetter
res = []
for k,v in groupby(sorted(d.items(),key=itemgetter(1)),key=itemgetter(1)):
    v = list(v)
    mx = max(v,key=lambda x:len(x[0]))
    res += [x for x in v if len(x[0]) == len(mx[0])]

res.sort()
for k,v in res:
    print(k,v)
('/a/b/c/d', [10, 22, 12])
('/e/f/h/i', [11, 28, 16])
('/j/k/l/m', [10, 22, 12])